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