Entity Framework Table Per Hierarchy Inserting Multiple Id Columns

点点圈 提交于 2019-12-11 12:36:16

问题


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();
          }
        }
      }

Context:

      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)
        {
        }
      }

Mapping:

      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));
        }
      }

Entities:

      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; }
      }

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


回答1:


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.

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>().



来源:https://stackoverflow.com/questions/32018833/entity-framework-table-per-hierarchy-inserting-multiple-id-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!