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