简体   繁体   English

EF代码优先-类型类型的属性

[英]EF Code First - Property of type Type

I have an object that has a property of type Type: 我有一个对象,其类型类型为:

public ScheduledJob
{
    public int ID { get; set; }
    public Type JobType { get; set; }
    public string JobParameters { get; set; }
}

When I generate the code-first migrations, I get the following error: 生成代码优先迁移时,出现以下错误:

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()

What is the best way to make this scenario work? 使此方案起作用的最佳方法是什么?

Edit for @NSGaga's post: 编辑@NSGaga的帖子:

The whole model (simplified) is this: 整个模型(简化的)是这样的:

All job objects implement the following interface: 所有作业对象都实现以下接口:

public interface IJob
{
    Guid ID { get; set; }
    void Run();
}

Each of the jobs has its own properties used as a sort of parameter: 每个作业都有自己的属性,用作某种参数:

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
    }
}

I use this model for an asynchronous job processing system, which works fine. 我将此模型用于异步作业处理系统,效果很好。 Now, I'm trying to build a scheduler, where I can give it a job type and parameters (serialized to string) and have it run on intervals. 现在,我正在尝试构建一个调度程序,在其中可以给它一个作业类型和参数(序列化为字符串)并使其定期运行。

Take a look at this post I made few days ago... 看看我几天前发表的这篇文章...

How should I define a field that can take various data types in EF? 我应该如何定义一个可以采用EF中各种数据类型的字段?

Why are you doing this ? 你为什么做这个 ?

You almost never need to save the Type as such. 您几乎不需要保存Type

@David mentioned already what to do. @David已经提到该怎么办。

But I'd further discourage you to go that way - but rather rethink. 但我会进一步劝阻您采取这种方式-而是重新考虑。

The EF code first is about having 'strong-typed' entities. EF代码首先是关于具有“强类型”实体的。 You can have nice 'inheritance' working, w/oa need to save a Type. 您可以很好地进行“继承”工作,而无需保存类型。

If you need something like an 'enum type' out of a limited # - use an enum or an int. 如果您需要使用有限的#中的“枚举类型”之类的东西,请使用枚举或整数。

You can post your model and I'll point you out if it could be changed. 您可以发布模型,如果可以更改,我会指出。


I think you could use inheritance for what you need. 我认为您可以根据需要使用继承。
Make different types of Jobs entities. 制作不同类型的Jobs实体。

eg take a look at this solution here (my post earlier) 例如在这里看看这个解决方案(我之前的帖子)
Multiple Inheritance Levels in EF Code Firs EF代码中的多个继承级别
...and let me know if problems, questions when you try something. ...让我知道是否有问题,尝试尝试时会提出疑问。

Maybe it's easier to use TPH (like it's in there), they all get stored in the same table - and you get less problems usually. 也许使用TPH会更容易(就像在其中一样),它们都存储在同一张表中-通常,您会遇到的问题更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉 EF 不要在 Code First 中更新引用类型属性? - How to tell EF not to Update Reference Type Property in Code First? 首先进行编码,属性“”上类型“”的ForeignKeyAttribute无效。 EF 6.1.3 - Code First The ForeignKeyAttribute on property '' on type '' is not valid. EF 6.1.3 在使用EF6和MVC 5的Code First迁移期间,navigation属性不是类型的声明属性 - The navigation property is not a declared property on type during Code First migration with EF6 and MVC 5 自定义值类型,EF代码优先和路由 - Custom value type, EF Code First and routing EF6:代码优先复杂类型 - EF6: Code First Complex Type EF代码首先无法将类型转换为int - EF code first cannot convert type to int EF代码优先:对继承的类型进行非规范化 - EF Code First: Denormalize Inherited Type EF代码优先-检测给定类型的一对多关系属性 - EF Code first - Detect one-to-many relationships property for given type 如何使用代码优先EF 4.3将复杂类型的属性映射到特定的表列? - How to map a property with a complex type to a specific table column with code-first EF 4.3? EF 4.1 Code First:类型中的每个属性名称必须是查找表关联上的唯一错误 - EF 4.1 Code First: Each property name in a type must be unique error on Lookup Table association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM