Problem: We have Order entity with some inherit types such as : OnlineOrder , OfflineOrder , ...
public class Order
{
public Order()
{
}
public virtual string Type { get; protected set; }
public string Title { get; set; }
public string Email { get; set; }
public string Description { get; set; }
public byte[] RowVersion { get; set; }
public ICollection<OrderDetail> Details { get; set; }
}
public class OnlineOrder : Order
{
public const string TypeName = "Online";
public OnlineOrder() : base()
{
}
public override string Type { get; protected set; } = TypeName;
public OnlineType OnlineType { get; set; }
public long FactorId { get; set; }
public bool IsConfirmed { get; set; } = false;
}
public class OfflineOrder : Order
{
public const string TypeName = "Offline";
public OfflineOrder() : base()
{
}
public override string Type { get; protected set; } = TypeName;
public InputType InputType { get; set; }
public long StoreId { get; set; }
}
and use this code in configuration of all entities:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
builder.ToTable(typeof(TEntity).Name, Schema);
}
but when run migration get this exception:
The entity type 'OffineOrder' cannot be mapped to a table because it is derived from 'Order'. Only base entity types can be mapped to a table.
Based this issue and this breaking change in ef core 3 ToTable()
throw an exception because(based breaking change link):
Starting with EF Core 3.0 and in preparation for adding TPT and TPC support in a later release,
ToTable()
called on a derived type will now throw an exception to avoid an unexpected mapping change in the future.
So we change configuration class:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
if (typeof(TEntity).BaseType == null)
builder.ToTable(typeof(TEntity).Name, Schema);
}