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

huangapple go评论87阅读模式
英文:

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&lt;OrderDetail&gt; Details { get; set; }
}

public class OnlineOrder : Order
{
    public const string TypeName = &quot;Online&quot;;
    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 = &quot;Offline&quot;;
    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&lt;TEntity&gt; builder)
{
	builder.ToTable(typeof(TEntity).Name, Schema);
}

but when run migration get this exception:

The entity type &#39;OffineOrder&#39; cannot be mapped to a table because it is derived from &#39;Order&#39;. 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&lt;TEntity&gt; 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().ToTable(null); 来解决了这个问题。

英文:

I got around this for now by calling modelBuilder.Entity<yourEntity>().ToTable(null);

huangapple
  • 本文由 发表于 2020年1月3日 19:35:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定