代码之家  ›  专栏  ›  技术社区  ›  user2877820

EF核心TPH鉴别器忽略基本实体

  •  0
  • user2877820  · 技术社区  · 6 年前

    我有两个DbContext。A BaseDbContext BaseDbContext 打电话 FemaleDbContext .

    public class BaseDbContext : DbContext
    {
        public BaseDbContext(DbContextOptions options) : base(options) { }
    
        public virtual DbSet<Person> Person { get; set; }
        public virtual DbSet<House> House { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>().ToTable("Person", "dbo");
            modelBuilder.Entity<House>().ToTable("House", "dbo");
    
            modelBuilder.Entity<Person>().HasOne(e => e.House).WithMany(e => e.Persons);
            modelBuilder.Entity<House>().HasMany(e => e.Persons).WithOne(e => e.House);
        }
    }
    

    目标是扩展 Person

    public class FemaleDbContext : BaseDbContext
    {
        public DbSet<Female> Female { get; set; }
    
        public FemaleDbContext(DbContextOptions<FemaleDbContext> options) : base(options) { }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Female>().HasBaseType<Person>();
    
            base.OnModelCreating(modelBuilder);
        }
    }
    

    Female 实体而不是 this.Context.Female.ToList() 在我的SubDbContext中,只有值为的实体 女性 Discriminator 在表中返回。但我想得到每一个实体。

    另外,我的实体如下:

    public class Person
    {
        public int Id { get; set; }
        public string Firstname { get; set; }
        public string Middlename { get; set; }
        public string Lastname { get; set; }
    }
    
    public class Female : Person
    {
        public bool? IsPregnant { get; set; }
    }
    

    这个。上下文。女性.ToList() 同时返回 Females Persons this.Context.Person.ToList() 已经归还了一切,不仅仅是 珀森斯

    0 回复  |  直到 6 年前
    推荐文章