![](/img/trans.png)
[英]How to tell EF not to Update Reference Type Property in Code First?
[英]EF Code First - Property of type Type
我有一个对象,其类型类型为:
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()
使此方案起作用的最佳方法是什么?
编辑@NSGaga的帖子:
整个模型(简化的)是这样的:
所有作业对象都实现以下接口:
public interface IJob
{
Guid ID { get; set; }
void 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
}
}
我将此模型用于异步作业处理系统,效果很好。 现在,我正在尝试构建一个调度程序,在其中可以给它一个作业类型和参数(序列化为字符串)并使其定期运行。
看看我几天前发表的这篇文章...
你为什么做这个 ?
您几乎不需要保存Type
。
@David已经提到该怎么办。
但我会进一步劝阻您采取这种方式-而是重新考虑。
EF代码首先是关于具有“强类型”实体的。 您可以很好地进行“继承”工作,而无需保存类型。
如果您需要使用有限的#中的“枚举类型”之类的东西,请使用枚举或整数。
您可以发布模型,如果可以更改,我会指出。
例如在这里看看这个解决方案(我之前的帖子)
EF代码中的多个继承级别
...让我知道是否有问题,尝试尝试时会提出疑问。
也许使用TPH会更容易(就像在其中一样),它们都存储在同一张表中-通常,您会遇到的问题更少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.