Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF Code First-类型的属性_C#_Ef Code First - Fatal编程技术网

C# EF Code First-类型的属性

C# EF Code First-类型的属性,c#,ef-code-first,C#,Ef Code First,我有一个具有类型属性的对象: public ScheduledJob { public int ID { get; set; } public Type JobType { get; set; } public string JobParameters { get; set; } } 在生成代码第一次迁移时,出现以下错误: System.ArgumentNullException: Value cannot be null. Parameter name: key

我有一个具有类型属性的对象:

public ScheduledJob
{
    public int ID { get; set; }
    public Type JobType { get; set; }
    public string JobParameters { get; set; }
}
在生成代码第一次迁移时,出现以下错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: key
    at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    at System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet, EdmEntityType entityType)
    at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
    at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
    at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
    at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
每个作业都有自己的属性,用作一种参数:

public class ProcessMedia : IJob
{
    public Guid ID { get; set; }

    public int MediaContentID { get; set; }

    public void Run()
    {
        if(MediaContentID <= 0)
             throw new Exception("Missing parameter MediaContentID");
        //work
    }
}
公共类ProcessMedia:IJob
{
公共Guid ID{get;set;}
公共int MediaContentID{get;set;}
公开募捐
{

如果(MediaContentID看看我几天前写的这篇文章

你为什么要这样做?

您几乎不需要保存
类型

@大卫已经提到了该做什么

但我会进一步劝阻你那样做——而是重新思考

EF代码首先是关于“强类型”实体的。你可以有很好的“继承”工作,而不需要保存类型

如果需要有限#中的“枚举类型”之类的内容,请使用枚举或int

你可以发布你的模型,如果可以修改,我会告诉你


我认为您可以使用继承来满足您的需要。
创建不同类型的作业实体

e、 g.在此处查看此解决方案(我之前的帖子)

…如果有问题,请在尝试时告诉我


也许使用TPH更容易(就像它在里面一样),它们都存储在同一个表中-通常问题较少。

将完全限定的类型名称保存为字符串。我以前做过此操作,只是检查是否有快捷方式。我更新了原始帖子,以显示更多的模型细节和推理。我将此标记为正确的教科书答案,但这不是正确的答案对于我正在构建的内容,最好的答案是。如果我使用继承,它将要求开发人员在每次创建新作业时都构建一个数据迁移脚本。我真的希望避免这种情况,因为我们可能会得到许多不同的作业类型。我同意@davin tryon的建议,存储程序集限定名并从中获取类型.没关系,每个问题都是特定的-您还可以添加一个“枚举”,每个类可以重新定义自己的唯一枚举值(前提是您/用户可以控制它,并且它是有限的)。这也只需要数据库中的一个“int”(或更少),而不是整个大字符串。但我也推荐David的答案作为您问题的最直接答案。
public class ProcessMedia : IJob
{
    public Guid ID { get; set; }

    public int MediaContentID { get; set; }

    public void Run()
    {
        if(MediaContentID <= 0)
             throw new Exception("Missing parameter MediaContentID");
        //work
    }
}