Migration exception when defining Db model with auto referred entities

I’m trying to define a DB through EF 3.1 (I’m forced to use this version, unfortunately) as follows.
Entities:

    public abstract class EntityBase
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int? AncestorId { get; set; }
        public virtual EntityBase Ancestor { get; set; }

        internal static void OnModelCreating<T>(ModelBuilder modelBuilder) where T : EntityBase
        {
            var entity = modelBuilder.Entity<T>();
            entity.HasKey(e => e.Id);

            entity.HasOne(e => e.Ancestor)
                .WithMany()
                .HasForeignKey(e => e.AncestorId)
                .OnDelete(DeleteBehavior.Restrict);
        }
    }

    public class EntityA:EntityBase
    {
        public string PropertyA { get; set; }


    }

    public class EntityB:EntityBase
    {
        public string PropertyB { get; set; }
    }

DbContext:

    public class TestDbContext : DbContext
    {
        private readonly string connectionString = // .... omissis;

        public DbSet<EntityA> EntitiesA { get; set; }
        public DbSet<EntityB> EntitiesB { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder); 
            optionsBuilder.UseSqlServer(connectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            EntityBase.OnModelCreating<EntityA>(modelBuilder);
            EntityBase.OnModelCreating<EntityB>(modelBuilder);

            //modelBuilder.Entity<EntityA>(entity =>
            //{
            //    entity.HasKey(e => e.Id);

            //    entity.HasOne(e => e.Ancestor)
            //        .WithMany()
            //        .HasForeignKey(e => e.AncestorId)
            //        .OnDelete(DeleteBehavior.Restrict);
            //});

            //modelBuilder.Entity<EntityB>(entity =>
            //{
            //    entity.HasKey(e => e.Id);

            //    entity.HasOne(e => e.Ancestor)
            //        .WithMany()
            //        .HasForeignKey(e => e.AncestorId)
            //        .OnDelete(DeleteBehavior.Restrict);
            //});
        }
    }

When I try the first migration I got this error:

Build started...
Build succeeded.
System.InvalidOperationException: A key cannot be configured on 'EntityA' because it is a derived type. The key must be configured on the root type 'EntityBase'. If you did not intend for 'EntityBase' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.
......

The target is to define 2 tables with a foreign key that refers an other entity in the same table, and I’d like to do it with a configuring method defined one time in the base class. In practice each entity shall be aware of its ancestor to implement a duplication logic.
I suspect this is due to the fact that EntityBase has a navigation property of type EntityBase and this creates some problem when a derived entity is going to be configured.
Maybe this problem can be fixed, I tried many modifications including configuring properties of each entity inside the DbContext without using generic static method, but nothing worked.

Any help is appreciated, thanks

[EDIT]

There’s indeed a way to do it, using generics as follows:

    public abstract class EntityBase<T> where T : EntityBase<T>
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int? AncestorId { get; set; }
        public T Ancestor { get; set; }

        internal static void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<T>();
            entity.HasKey(e => e.Id);

            entity.HasOne(e => e.Ancestor)
                .WithMany()
                .HasForeignKey(e => e.AncestorId)
                .OnDelete(DeleteBehavior.Restrict);
        }
    }

    public class EntityA:EntityBase<EntityA>
    {
        public string PropertyA { get; set; }


    }

    public class EntityB:EntityBase<EntityB>
    {
        public string PropertyB { get; set; }
    }

DbContext:

    public class TestDbContext : DbContext
    {
        private readonly string connectionString = // .... omissis;

        public DbSet<EntityA> EntitiesA { get; set; }
        public DbSet<EntityB> EntitiesB { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder); 
            optionsBuilder.UseSqlServer(connectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            EntityBase<EntityA>.OnModelCreating(modelBuilder);
            EntityBase<EntityB>.OnModelCreating(modelBuilder);
        }
    }

EF is detecting the entities even if you don’t use them as DbSet, so in this case, I think you need to Ignore the object like this:

modelBuilder.Ignore<EntityBase>();

Based on your comment, seems your design models is wrong, keep in mind for EF a relation means a database Table which says that EnitityBase must be a table where it’s not!
Don’t mix C# inheritance concepts with EF core, I would suggest starting with this design if it works try to make it nice!

public abstract class EntityBase
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class EntityA : EntityBase
{
    public string PropertyA { get; set; }

    public int? AncestorId { get; set; }
    public virtual EntityA Ancestor { get; set; }

    internal static void OnModelCreating(ModelBuilder modelBuilder)
    {
        var entity = modelBuilder.Entity<EntityA>();
        entity.HasKey(e => e.Id);

        entity.HasOne(e => e.Ancestor)
            .WithMany()
            .HasForeignKey(e => e.AncestorId)
            .OnDelete(DeleteBehavior.Restrict);
    }

}

public class EntityB : EntityBase
{
    public string PropertyB { get; set; }

    public int? AncestorId { get; set; }
    public virtual EntityB Ancestor { get; set; }

    internal static void OnModelCreating(ModelBuilder modelBuilder)
    {
        var entity = modelBuilder.Entity<EntityB>();
        entity.HasKey(e => e.Id);

        entity.HasOne(e => e.Ancestor)
            .WithMany()
            .HasForeignKey(e => e.AncestorId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật