c# - EF Core 6.0 无效操作异常 : The object has been removed from the model

标签 c# entity-framework-core .net-6.0

正在将 WebApi 从 .net5 升级到 .net6。在名为“Order”的域实体的实体配置期间遇到了与 EF Core 6.0 相关的以下异常,该域实体与“ChargeItems”具有一对多关系:

System.InvalidOperationException HResult=0x80131509 Message=The object has been removed from the model. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.get_Builder() at Microsoft.EntityFrameworkCore.Metadata.Internal.Navigation.OnAnnotationSet(String name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation) at Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.OnAnnotationSet(String name, Annotation annotation, Annotation oldAnnotation) at Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase.SetAnnotation(String name, Annotation annotation, Annotation oldAnnotation) at Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetAnnotation(String name, Object value, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetOrRemoveAnnotation(String name, Object value, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.SetPropertyAccessMode(Nullable1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable1 propertyAccessMode) at Eventec.Persistence.Core.Orders.OrderConfiguration.Configure(EntityTypeBuilder1 builder) in E:\Project\PersisentenceCore\Orders\OrderConfiguration.cs:line 101 at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration1 configuration)

以下代码遇到此异常:

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 上运行得很好。

最佳答案

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

请前往 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);

关于c# - EF Core 6.0 无效操作异常 : The object has been removed from the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69917518/

相关文章:

c# - 将 System.Text.Json 源生成器与自定义 JsonConverter 结合使用

c# - 字符串排序陷阱

entity-framework - 取该组的每个第一个元素

c# - 迁移中未应用的最大长度

c# - ReflectionTypeLoadException 在程序包管理器控制台中运行添加迁移命令时出错

Dotnet Maui 中的 Android Assets 交付

C# 序列化不会向 xml 文件中的属性添加值

c# - 带有术语汇总的Elasticsearch日期直方图报告

javascript - Ajax POST 数据未出现在模式中

c# - 连续接收来自秤的数据