2015-04-23 57 views
1

我想用自己的常量(而不是EF的字符串常量)替換鑑別器列。我班如下:鏈接繼承的EF鑑別器

[Table("stor_store")] 
public abstract StoreBase { /* Base fields */ } 

public StoreTemplate : StoreBase {/* Extra fields */ } 

public Store : StoreBase {/* Extra fields */ } 

[Table("cust_customer")] 
public Customer : Store { /* Extra fields */ } 

[Table("engi_engineer")] 
public Engineer : Store {/* Extra fields */ } 

我一直在試圖映射使用流利的API列:

modelBuilder.Entity<StoreBase>() 
    .Map<StoreTemplate>(m => m.Requires("stor_type").HasValue((byte)0) 
    .Map<Store>(m => m.Requires("stor_type").HasValue((byte)1)) 
    .Map<Customer>(m => m.Requires("stor_type").HasValue((byte)2)) 
    .Map<Engineer>(m => m.Requires("stor_type").HasValue((byte)3)); 

但是EF總是創建鑑別列。這個映射之前已經有效,但是雙嵌套繼承似乎已經拋出了它。謝謝你的幫助。

回答

1

我可以在這裏看到你的問題,我想這是因爲Store已經被分配了一個值,所以也許當你試圖爲繼承的實體設置它失敗的值?

您可以嘗試不同的順序:重新排序並沒有幫助!

modelBuilder.Entity<StoreBase>() 
.Map<StoreTemplate>(m => m.Requires("stor_type").HasValue((byte)0) 
.Map<Customer>(m => m.Requires("stor_type").HasValue((byte)2)) 
.Map<Engineer>(m => m.Requires("stor_type").HasValue((byte)3)) 
.Map<Store>(m => m.Requires("stor_type").HasValue((byte)1)); 

另一種方法

然而,當你的產業變得更加複雜,也許你應該考慮實施類似的「表每類型」(TPT)策略?這種方法會將你的類型分離到它們自己的表格中,保存在較小的鑑別器的空列上,並給你一個更清晰的結構。

enter image description here

你可以找到一個完整描述的細節在這裏:http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt

+1

我並沒有解決我的問題,鑑別仍然存在重新排序後的地圖。但我想我會選擇TPT。 – Tim