Reputation: 2488
I've defined the following classes:
public class DeletableEntity
{
public bool IsDeleted { get; set; }
}
public class ClassA : DeletableEntity
{
}
public class ClassB : ClassA
{
}
In the OnModelCreating I'm getting only entities that inherit from DeletableEntity like this:
foreach (var entityType in builder.Model.GetEntityTypes())
{
if (typeof(DeletableEntity).IsAssignableFrom(entityType.ClrType) == true)
{
var et = entityType.BaseType;
}
}
Interestingly ClassA has entityType.BaseType == null but ClrType.BaseType clearly shows ClassA inherits from DeletableEntity:
For ClassB entityType.BaseType is ClassA as expected:
Why first successor's parent class is being ignored? Is this by design?
PS: I'm using EF Core 2.2.
Upvotes: 1
Views: 544
Reputation: 205849
It's because there is a difference between base class and base entity.
IEntityType
represents class mapped as EF Core entity. The type BaseType
property of IEnityType
is also IEnityType
. But in your example DeletableEntity
is just a base class, not a base entity, so the property returns null
.
Probably the best explanation is the property declaration:
//
// Summary:
// Gets the base type of the entity. Returns null if this is not a derived type
// in an inheritance hierarchy.
IEntityType BaseType { get; }
where "inheritance hierarchy" refers to EF Inheritance:
Inheritance in the EF model is used to control how inheritance in the entity classes is represented in the database.
and also Inheritance (Relational Database).
Upvotes: 4