Ef Core 3实体类型XOrder不能映射到表,因为它是从Order派生的。只能将基本实体类型映射到表

哈默德·内美

问题:我们的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.
哈默德·内美

基于此问题ef核心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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档