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评论112阅读模式
英文:

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,...

  1. public class Order
  2. {
  3. public Order()
  4. {
  5. }
  6. public virtual string Type { get; protected set; }
  7. public string Title { get; set; }
  8. public string Email { get; set; }
  9. public string Description { get; set; }
  10. public byte[] RowVersion { get; set; }
  11. public ICollection<OrderDetail> Details { get; set; }
  12. }
  13. public class OnlineOrder : Order
  14. {
  15. public const string TypeName = "Online";
  16. public OnlineOrder() : base()
  17. {
  18. }
  19. public override string Type { get; protected set; } = TypeName;
  20. public OnlineType OnlineType { get; set; }
  21. public long FactorId { get; set; }
  22. public bool IsConfirmed { get; set; } = false;
  23. }
  24. public class OfflineOrder : Order
  25. {
  26. public const string TypeName = "Offline";
  27. public OfflineOrder() : base()
  28. {
  29. }
  30. public override string Type { get; protected set; } = TypeName;
  31. public InputType InputType { get; set; }
  32. public long StoreId { get; set; }
  33. }

并在所有实体的配置中使用以下代码:

  1. public virtual void Configure(EntityTypeBuilder<TEntity> builder)
  2. {
  3. builder.ToTable(typeof(TEntity).Name, Schema);
  4. }

但是运行迁移时出现以下异常:

  1. 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 , ...

  1. public class Order
  2. {
  3. public Order()
  4. {
  5. }
  6. public virtual string Type { get; protected set; }
  7. public string Title { get; set; }
  8. public string Email { get; set; }
  9. public string Description { get; set; }
  10. public byte[] RowVersion { get; set; }
  11. public ICollection&lt;OrderDetail&gt; Details { get; set; }
  12. }
  13. public class OnlineOrder : Order
  14. {
  15. public const string TypeName = &quot;Online&quot;;
  16. public OnlineOrder() : base()
  17. {
  18. }
  19. public override string Type { get; protected set; } = TypeName;
  20. public OnlineType OnlineType { get; set; }
  21. public long FactorId { get; set; }
  22. public bool IsConfirmed { get; set; } = false;
  23. }
  24. public class OfflineOrder : Order
  25. {
  26. public const string TypeName = &quot;Offline&quot;;
  27. public OfflineOrder() : base()
  28. {
  29. }
  30. public override string Type { get; protected set; } = TypeName;
  31. public InputType InputType { get; set; }
  32. public long StoreId { get; set; }
  33. }

and use this code in configuration of all entities:

  1. public virtual void Configure(EntityTypeBuilder&lt;TEntity&gt; builder)
  2. {
  3. builder.ToTable(typeof(TEntity).Name, Schema);
  4. }

but when run migration get this exception:

  1. 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() 现在会抛出异常,以避免未来的意外映射更改。

因此,我们更改配置类:

  1. public virtual void Configure(EntityTypeBuilder<TEntity> builder)
  2. {
  3. if (typeof(TEntity).BaseType == null)
  4. builder.ToTable(typeof(TEntity).Name, Schema);
  5. }
英文:

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:

  1. public virtual void Configure(EntityTypeBuilder&lt;TEntity&gt; builder)
  2. {
  3. if (typeof(TEntity).BaseType == null)
  4. builder.ToTable(typeof(TEntity).Name, Schema);
  5. }

答案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:

确定