简体   繁体   English

使用实体框架TPT的多个继承级别

[英]Multiple Inheritance Levels Using Entity Framework TPT

Considering the following table schema: 考虑以下表模式:

在此处输入图片说明

I want to use Entity Framework Table-Per-Type (Fluent API) to map to the following class diagram: 我想使用实体框架按类型的表(Fluent API)映射到以下类图:

在此处输入图片说明

How can I do this? 我怎样才能做到这一点? I have tried doing it like the examples at the Microsoft site but looks like no example covers a two-depth inheritance scenario and I get errors related to invalid discriminator columns and so on. 我曾尝试像Microsoft网站上的示例一样进行操作,但似乎没有示例涵盖两层继承方案,并且出现了与无效区分符列相关的错误,依此类推。 I am using one EntityTypeConfiguration per type. 我为每种类型使用一个EntityTypeConfiguration How can I specify the name of the foreign key cascading down children? 如何指定级联孩子的外键名称?

If anyone has an example of how to do such mapping, I would be grateful forever. 如果有人有如何进行这种映射的例子,我将永远感激。

Thanks! 谢谢!

So your table per type won't quite work for your schema because table per type links tables together through a shared primary key. 因此,每种类型的表对您的架构不太有效,因为每种类型的表通过共享主键将表链接在一起。 You could modify your schema so that UserAccounts, ExternalUserAccounts, and SystemUserAccounts are all keyed on UserAccountId. 您可以修改架构,以便将UserAccounts,ExternalUserAccounts和SystemUserAccounts都键入到UserAccountId上。 Then the code below is close to what you want. 然后,下面的代码接近您想要的代码。 Otherwise, you'll want to use regular association properites. 否则,您将需要使用常规关联属性。

Read about table per type here and shared primary keys here . 阅读关于每个类型的表在这里和共享主键在这里

    public class UserAccount
    {
        public int UserAccountId {get;set;}
        public int UserId {get;set;}
        public DateTime LastLogin {get;set;}
        public bool IsLocked {get;set;}
        public bool IsActive {get;set;}
        public User User {get;set;}
    }

    public class ExternalUserAccount : UserAccount
    {
        public int ExternalAccountId {get;set;}
        public string ProviderName {get;set;}
        public string ProviderUserName {get;set;}
    }

    public class SystemUserAccount : UserAccount
    {
        public int SystemUserAccountId {get;set;}
        public string PasswordHash {get;set;}
        public string Token {get;set;}
    }

    public class User
    {
        public int UserId {get;set;}
        public string Name {get;set;}
        public string Description {get;set;}
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserAccount>().ToTable("UserAccounts");
        modelBuilder.Entity<UserAccount>().HasKey(ua => ua.UserAccountId);
        modelBuilder.Entity<UserAccount>().HasRequired(ua => ua.User)
                                          .WithMany()
                                          .HasForeignKey(ua => ua.UserId);
        modelBuilder.Entity<ExternalUserAccount>().ToTable("ExternalUserAccounts");
        modelBuilder.Entity<SystemUserAccount>().ToTable("SystemUserAccounts");
        modelBuilder.Entity<User>().ToTable("Users");
        modelBuilder.Entity<User>().HasKey(u => u.UserId);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM