【发布时间】:2021-11-10 17:15:17
【问题描述】:
我正在将 WebApi 从 .net5 升级到 .net6。在名为“Order”的域实体的实体配置过程中遇到以下与 EF Core 6.0 相关的异常,该域实体与“ChargeItems”具有一对多关系:
System.InvalidOperationException H结果=0x80131509 Message=对象已从模型中移除。 源=Microsoft.EntityFrameworkCore 堆栈跟踪: 在 Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.get_Builder() 在 Microsoft.EntityFrameworkCore.Metadata.Internal.Navigation.OnAnnotationSet(字符串名称,IConventionAnnotation 注释,IConventionAnnotation oldAnnotation) 在 Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.OnAnnotationSet(字符串名称,注释注释,注释 oldAnnotation) 在 Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.SetAnnotation(字符串名称,注释注释,注释 oldAnnotation) 在 Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetAnnotation(字符串名称,对象值,ConfigurationSource 配置源) 在 Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetOrRemoveAnnotation(字符串名称、对象值、ConfigurationSource 配置源) 在 Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.SetPropertyAccessMode(Nullable
1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable1 propertyAccessMode) 在 Eventec.Persistence.Core.Orders.OrderConfiguration.Configure(EntityTypeBuilder1 builder) in E:\Project\PersisentenceCore\Orders\OrderConfiguration.cs:line 101 at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration1 配置)
在以下代码中遇到此异常:
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.ToTable("orders").HasKey(r => r.Id);
// other property config etc
// Charge Items
builder.HasMany(p => p.ChargeItems)
.WithOne()
.HasForeignKey("order_id")
.HasConstraintName("charge_items_order_id_fk")
.IsRequired()
.OnDelete(DeleteBehavior.Cascade)
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
无法通过使用 Google 等的故障排除帮助找到很多东西。也没有任何关于使用 EF Core 6 进行重大更改来解决此问题的信息。当然,它在 EF Core 5.x 上运行得很好。
【问题讨论】:
-
所以没有对 order 或 chargeitem 类进行任何更改?数据库中是否存在外键约束?也许尝试复制这些表,然后从中删除所有现有数据。然后针对这些运行应用程序?可能正在尝试进行更改,但由于现有数据限制而被阻止
标签: c# entity-framework-core .net-6.0