visual-studio-2017 - 在 EF Core、ASP.NET Core MVC 中实现 Table Per Hierarchy Inheritance 的最佳方法
问题描述
在我的网站开发过程中,我决定使用一个基类并从中派生两个其他类。
我的基类是
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gendre { get; set; }
public string CityOfBirth { get; set; }
public string ProvinceOfBirth { get; set; }
public string FiscalCode { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
public Address Address { get; set; }
public string AdditionaInfos { get; set; }
public string CountryOfBirth { get; set; }
}
请注意,我没有在我的基类中使用任何 ID。也许已经是一个错误。这是因为我在第二步中添加了基类,并且在派生类之前已经分配了 ID。
其他派生类是:
public class Loaner : Person
{
public int ID { get; set; }
public int AID { get; set; }
public ICollection<A> A{ get; set; }
// user ID from AspNetUser table
public string OwnerID { get; set; }
}
和
public class Client : Person
{
public int ID { get; set; }
public string TypeOfLocator { get; set; }
public bool IsRegistered { get; set; } = false;
public bool TakeDataFromRegistration { get; set; } = false;
public bool WantsToBeContactedByEmail { get; set; } = false;
[DataType(DataType.EmailAddress)]
public string EMailAddress { get; set; }
}
我的 DbContext 看起来像这样:
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
public DbSet<Client> Clients { get; set; }
public DbSet<Loaner> Loaners { get; set; }
public DbSet<Address> Address{ get; set; }
}
现在,如果我知道我的基类中没有 ID,我怎样才能最好地实现 TPH?我的问题是派生类的 ID 已经在我的网站的其余部分中广泛使用。
我看了一下here,但是这种方法需要在基类上有ID。我真的必须创建和自定义迁移代码吗?或者像这里描述的那样使用流利的 API 就足够了吗?
任何帮助表示赞赏。