[英]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(Nullable1 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(EntityTypeBuilder1 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(EntityTypeBuilder1 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.