每个层次结构的实体框架表插入多个ID列

问题描述:

我认真地花了两个工作日来尝试从数据库优先到代码优先的TPH设置.我得到的错误是类似无效的列名Entity_EntityId/Entity_Entity_Id1"之类的

I have seriously spent two work days trying to a TPH setup from Database First to Code first. The Error I get is Something like "Invalid Column Name Entity_EntityId/ Entity_Entity_Id1"

我已经像这样草拟了一个非常基本的问题:

I've drawn up a very basic reproduction of the issue like so:

      internal class Program
      {
        private static void Main(string[] args)
        {
          using (var context = new Context())
          {
            var baseClass = new Base {Name = "Test"};
            context.BaseClasses.Add(baseClass);
            context.SaveChanges();
            var baseClasses = context.BaseClasses.ToList();
          }
        }
      }

上下文:

      public class Context : DbContext
      {
        public Context() : base("TPH")
        {
        }

        public DbSet<Base> BaseClasses { get; set; }
        public DbSet<Derived> DervDerivedClasses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
      }

映射:

      public class BaseMap : EntityTypeConfiguration<Base>
      {
        public BaseMap()
        {
          HasKey(b => b.Id);

          Property(b => b.Name);
          HasOptional(b => b.AnotherClass)
            .WithMany(b => b.Bases)
            .HasForeignKey(b => b.AnotherClassId);

          Map(b => b.Requires("Disc").HasValue(1));
        }
      }

      public class DerivedMap : EntityTypeConfiguration<Derived>
      {
        public DerivedMap()
        {
          HasKey(b => b.Id);

          Property(b => b.Name);
          HasOptional(b => b.AnotherClass)
            .WithMany(b => b.Deriveds)
            .HasForeignKey(b => b.AnotherClassId);

          Map(b => b.Requires("Disc").HasValue(2));
        }
      }

      public class SecondDerivedMap : EntityTypeConfiguration<SecondDerived>
      {
        public SecondDerivedMap()
        {
          HasKey(b => b.Id);

          Property(b => b.Name);

          HasOptional(b => b.AnotherClass)
            .WithMany(b => b.SecondDeriveds)
            .HasForeignKey(b => b.AnotherClassId);

          Map(b => b.Requires("Disc").HasValue(3));
        }
      }

实体:

      public class Base
      {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? AnotherClassId { get; set; }
        public AnotherClass AnotherClass { get; set; }
      }

      public class Derived : Base
      {
      }

      public class SecondDerived : Base
      {
      }

      public class AnotherClass
      {
        public int Id { get; set; }
        public ICollection<Base> Bases { get; set; }
        public ICollection<Derived> Deriveds { get; set; }
        public ICollection<SecondDerived> SecondDeriveds { get; set; }
      }

如何使表格只有一个"AnotherClassId"?

How can I get the table to just have a single "AnotherClassId?"

每个关系的每个实体只应具有一个导航属性-并且您拥有三个(BasesDeriveds). EF看到了这些属性,并认为AnotherClassBase层次结构中的各个类之间存在三种不同的一对多关联.

You're only supposed to have a single navigation property per entity per relationship -- and you have three (Bases, Deriveds, and SecondDeriveds). EF sees those properties and thinks there are three different one-to-many associations between AnotherClass and the various classes in the Base hierarchy.

如果您想从AnotherClass获取相关的Derived实体的集合,则应该使用anotherClassEntity.Bases.OfType<Derived>()之类的东西.

If you want to get a collection of the related Derived entities from AnotherClass, you're supposed to use something like anotherClassEntity.Bases.OfType<Derived>().