【问题标题】:EF Core 6.0 InvalidOperationException: The object has been removed from the modelEF Core 6.0 InvalidOperationException:对象已从模型中删除
【发布时间】: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(Nullable1 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


【解决方案1】:

新版本、新错误/重大更改。

请前往 EF Core GitHub Issue Tracker 填写问题/错误报告。

有问题的调用是

.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);

作为一种解决方法,要么完全删除它(它应该是默认设置),要么使用 EF Core 5.0 引入的 Navigation fluent API 进行配置:

builder.HasMany(p => p.ChargeItems)
    .WithOne()
    .HasForeignKey("order_id")
    .HasConstraintName("charge_items_order_id_fk")
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);
//.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);

builder.Navigation(p => p.ChargeItems)
    .UsePropertyAccessMode(PropertyAccessMode.Field);

【讨论】:

  • 好的,谢谢回复。我填写了一份错误报告:github.com/dotnet/efcore/issues/26611
  • 我为我所有类似配置的一对多属性实现了您的解决方法,它就像一个魅力。太感谢了。伤口自己从来没有想过。
猜你喜欢
  • 2021-07-22
  • 2018-12-16
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多