0
我在將Fluent API擴展到繼承類時遇到了問題。我採用了TPT(table per type)方法,並且每種類型的繼承都有一個表。我喜歡每種類型的表格,因爲數據庫完全標準化並易於維護。我沒有得到繼承的模型ServiceCompany
與Fluent API一起工作。將TPT繼承代碼第一個模型添加到Linq Fluent API
抽象基類
public abstract class Vendor
{
[Key]
public int VendorID { get; set; }
[Required]
public string CompanyName { get; set; }
[Required]
public int StreetNumber { get; set; }
[Required]
public string StreetName { get; set; }
}
繼承ServiceCompany類從供應商
當我加入了實體模型,以便能夠與onModelCreating()的流暢API
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Vendor> Vendors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ServiceCompany>().ToTable("ServiceCompanies");
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
我會喜歡用流利的API來做這樣的事情。
var ListofServiceCompanies = db.ServiceCompanies.All()
,而不是像這樣
var ListofServiceCompanies = db.Vendor.SelectMany(Vendor is a ServiceComapny...etc)
我寧願正確設置實體和使代碼漂亮,使用方便。任何見解或知識,表示讚賞。
爲T設置Fluent API的很好的解釋PT(Table per Type)繼承 – dev8989