Ham*_*aei 5 c# entity-framework .net-core asp.net-core ef-core-3.0
问题:我们有一些继承类型的 Order 实体,例如: 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; }
}
Run Code Online (Sandbox Code Playgroud)
并在所有实体的配置中使用此代码:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
builder.ToTable(typeof(TEntity).Name, Schema);
}
Run Code Online (Sandbox Code Playgroud)
但是当运行迁移时得到这个异常:
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.
Run Code Online (Sandbox Code Playgroud)
基于此问题和 ef core 3 中的此重大更改ToTable()
会引发异常,因为(基于重大更改链接):
从 EF Core 3.0 开始,并准备在以后的版本中添加 TPT 和 TPC 支持,
ToTable()
调用派生类型现在将引发异常,以避免将来出现意外的映射更改。
所以我们改变配置类:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
if (typeof(TEntity).BaseType == null)
builder.ToTable(typeof(TEntity).Name, Schema);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5009 次 |
最近记录: |