gpt4 book ai didi

c# - Ef Core 3 实体类型 XOrder 不能映射到表,因为它派生自 Order Only 基本实体类型可以映射到表

转载 作者:行者123 更新时间:2023-12-03 16:50:10 28 4
gpt4 key购买 nike

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; }
}

并在所有实体的配置中使用此代码:
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.

最佳答案

基于 this issuethis breaking change在 ef 核心 3 ToTable()抛出异常,因为( 基于中断更改链接 ):

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.



所以我们改变配置类:
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
if (typeof(TEntity).BaseType == null)
builder.ToTable(typeof(TEntity).Name, Schema);
}

关于c# - Ef Core 3 实体类型 XOrder 不能映射到表,因为它派生自 Order Only 基本实体类型可以映射到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com