在EF Core,ASP.NET Core MVC中实现按层次结构继承表的最佳方法

时间:2018-06-30 01:35:01

标签: visual-studio-2017 asp.net-core-mvc entity-framework-core

在开发网站期间,我决定使用基类并从中派生另外2个基类。

我的基础课是

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。 我真的必须创建和自定义迁移代码吗?还是使用像here所述的流利的API就足够了?

感谢您的帮助。

0 个答案:

没有答案
相关问题