英文:
Ef Core 3 The entity type XOrder cannot be mapped to a table because it is derived from Order Only base entity types can be mapped to a table
问题
问题:我们有一个名为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; }
}
并在所有实体的配置中使用以下代码:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
builder.ToTable(typeof(TEntity).Name, Schema);
}
但是运行迁移时出现以下异常:
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.
英文:
> 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.
答案1
得分: 5
根据这个问题和这个重大变更,在 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);
}
英文:
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);
}
答案2
得分: 0
另一个原因是在子实体(继承自另一个实体)上使用 SetTableName
。请参阅此问题。
英文:
Another cause is using SetTableName
on a child entity (inherited from another entity). See this issue
答案3
得分: -2
我目前通过调用 modelBuilder.Entity
英文:
I got around this for now by calling modelBuilder.Entity<yourEntity>().ToTable(null);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论