EF代码第一 - 类型类型的属性
我有一个对象的属性类型为:
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?
为@ NSGaga的帖子编辑:
整个模型)是这样的:
所有作业对象都实现以下界面:
All job objects implement the following interface:
public interface IJob
{
Guid ID { get; set; }
void Run();
}
每个作业都有自己的属性用作一种参数: / p>
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...
你为什么这样做?
你几乎不需要保存类型
。
@David提到了已经做了什么。
@David mentioned already what to do.
但是,我进一步阻止你这样做 - 而是反思。
But I'd further discourage you to go that way - but rather rethink.
EF代码首先是关于强类型实体。你可以有很好的'继承'工作,无需保存类型。
The EF code first is about having 'strong-typed' entities. You can have nice 'inheritance' working, w/o a need to save a Type.
如果您需要某种类似枚举类型的限制#使用枚举或int。
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.
例如看看这个解决方案在这里(我的帖子)
EF代码保护区中的多个继承级别
...并且让我知道问题,当您尝试某些问题时。
e.g. take a look at this solution here (my post earlier)
Multiple Inheritance Levels in EF Code Firs
...and let me know if problems, questions when you try something.
也许它更容易使用TPH(就像在那里),它们都存储在同一张表中 - 通常你会收到较少的问题。
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.