2013-04-12 41 views
0

我有了Type類型的屬性的對象:EF代碼優先 - 型物業類型

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

我將此模型用於異步作業處理系統,效果很好。現在,我試圖構建一個調度程序,在那裏我可以給它一個作業類型和參數(序列化爲字符串),並讓它按間隔運行。

+1

將完全限定的類型名稱保存爲字符串。 –

+0

我之前是這麼做的,我只是在檢查是否有捷徑。 –

回答

1

看看這個帖子我前幾天發...

How should I define a field that can take various data types in EF?

你爲什麼要這麼做?

您幾乎從不需要保存Type

@David mentioned already what to do.

但是,我會進一步阻止你這樣做 - 而是重新思考。

EF代碼首先是關於「強類型」的實體。你可以有很好的'繼承'工作,而不需要保存一個類型。

如果您需要類似'枚舉類型'之外的有限#號 - 使用枚舉或int。

你可以發佈你的模型,我會指出你是否可以改變。


我認爲你可以使用繼承爲你所需要的。
製作不同類型的作業實體。

例如看看這裏的解決方案(我的帖子更早)
Multiple Inheritance Levels in EF Code Firs
...並讓我知道如果問題,當你嘗試的東西的問題。

也許它更容易使用TPH(就像它在那裏),它們都被存儲在同一張表中 - 並且通常會得到更少的問題。

+0

我已更新原始帖子以顯示更多模型細節和推理。 –

+0

我更新了 - 看一看,讓我知道。 – NSGaga

+0

我將此標記爲正確的教科書答案,但它不是我正在構建的最佳答案。如果我使用繼承,則每次創建新作業時都需要開發人員構建數據遷移腳本。我真的想避免這種情況,因爲我們可能會有許多不同的工作類型。我使用@ davin-tryon的建議去存儲程序集限定名並從中獲取類型。 –