简体   繁体   English

EF Core 6.0 InvalidOperationException:对象已从模型中删除

[英]EF Core 6.0 InvalidOperationException: The object has been removed from the model

Am in the process of upgrading a WebApi from .net5 to .net6.我正在将 WebApi 从 .net5 升级到 .net6。 Am hitting a wall with the following exception related to EF Core 6.0 during the Entity Configuration of a domain entity called "Order" which has a one-to-many relationship to "ChargeItems":在名为“Order”的域实体的实体配置期间遇到了与 EF Core 6.0 相关的以下异常,该域实体与“ChargeItems”具有一对多关系:

System.InvalidOperationException HResult=0x80131509 Message=The object has been removed from the model. System.InvalidOperationException HResult=0x80131509 消息=对象已从模型中删除。 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(Nullable 1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable Source=Microsoft.EntityFrameworkCore StackTrace:在 Microsoft.EntityFrameworkCore.Metadata.Internal.ForeignKey.get_Builder() 在 Microsoft.EntityFrameworkCore.Metadata.Internal.Navigation.OnAnnotationSet(String name, IConventionAnnotation annotation, IConventionAnnotation oldAnnotation) 在 Microsoft.EntityFrameworkCore.Infrastructure。 ConventionAnnotable.OnAnnotationSet(String name, Annotation annotation, Annotation oldAnnotation) at Microsoft.EntityFrameworkCore.Infrastructure.AnnotableBase.SetAnnotation(String name, Annotation annotation, Annotation oldAnnotation) at Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetAnnotation(String name, Object value) ConfigurationSource configurationSource) 在 Microsoft.EntityFrameworkCore.Infrastructure.ConventionAnnotatable.SetOrRemoveAnnotation(String name, Object value, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.SetPropertyAccessMode(Nullable 1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable 1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable 1 propertyAccessMode) at Eventec.Persistence.Core.Orders.OrderConfiguration.Configure(EntityTypeBuilder 1 builder) in E:\\Project\\PersisentenceCore\\Orders\\OrderConfiguration.cs:line 101 at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration 1 configuration) 1 propertyAccessMode, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IMutablePropertyBase.SetPropertyAccessMode(Nullable 1 propertyAccessMode) at Eventtec.Persistence.Core.Orders.OrderConfiguration.Configure(EntityTypeBuilder 1 builder) in E:\\Project\\PersisentenceCore\\Orders\\OrderConfiguration.cs:line 101 at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration 1 配置)

This exception is being hit at the following code:在以下代码中遇到此异常:

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);


    }
}

Cant find much by way of troubleshooting help using The Google etc. Nor anything about breaking changes with EF Core 6 to account for this.无法通过使用 Google 等的故障排除帮助找到太多内容。也没有任何关于使用 EF Core 6 进行重大更改来解决此问题的信息。 Of course it was working just fine with EF Core 5.x.当然,它与 EF Core 5.x 一起工作得很好。

New version, new bugs/breaking changes.新版本,新错误/重大更改。

Please go and fill issue/bug report at EF Core GitHub Issue Tracker .请前往 EF Core GitHub 问题跟踪器填写问题/错误报告。

The problematic call is有问题的电话是

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

As a workaround, either remove it completely (it should be the default), or configure it with the EF Core 5.0 introduced Navigation fluent API:作为一种解决方法,要么完全删除它(它应该是默认值),要么使用 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core“InvalidOperationException: Include has been used on non entity queryable”用于导航属性,即使在显式包含之后 - EF Core "InvalidOperationException: Include has been used on non entity queryable" for navigation property even after explicit include IQueryable 上的 EF Core “InvalidOperationException:InvalidOperationException:Include 已用于非实体可查询” - EF Core “InvalidOperationException: Include has been used on non entity queryable” on IQueryable EF Core 异步 select InvalidOperationException - EF Core async select InvalidOperationException EF:检查对象是否已被修改 - EF: checking if object has been modified 对象实例已处置(EF) - Object Instance has been disposed (EF) 如果已调用对象的终结器,那么我是否可以100%确定之后将/将其从内存中删除? - If an object's finalizer has been called, can I be 100% sure that it is/will be removed from memory afterwards? LINQPad EF Core 没有为此 DbContext 配置数据库提供程序 - LINQPad EF Core no database provider has been configured for this DbContext EF Core 2.1 - 没有为此 DbContext 配置数据库提供程序 - EF Core 2.1 - No database provider has been configured for this DbContext 在内存中使用 SQLite 运行 EF Core - 未配置数据库提供程序 - Running EF Core with SQLite in memory - No Database Provider has been configured DropCreateDatabaseIfModelChanges EF6导致System.InvalidOperationException:支持上下文的模型已更改 - DropCreateDatabaseIfModelChanges EF6 results in System.InvalidOperationException: The model backing the context has changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM