EF Core 6.0 InvalidOperationException:对象已从模型中移除
EF Core 6.0 InvalidOperationException: The object has been removed from the model
我正在将 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(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)
在以下代码中遇到此异常:
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.
一起工作得很好
新版本,新 bugs/breaking 变化。
请前往 EF Core GitHub Issue Tracker 填写 issue/bug 报告。
有问题的调用是
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
作为解决方法,要么完全删除它(它应该是默认设置),要么使用引入的 EF Core 5.0 配置它 Navigation
流畅 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);
我正在将 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(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)
在以下代码中遇到此异常:
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.
一起工作得很好新版本,新 bugs/breaking 变化。
请前往 EF Core GitHub Issue Tracker 填写 issue/bug 报告。
有问题的调用是
.Metadata.PrincipalToDependent.SetPropertyAccessMode(PropertyAccessMode.Field);
作为解决方法,要么完全删除它(它应该是默认设置),要么使用引入的 EF Core 5.0 配置它 Navigation
流畅 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);