[英]Owned entity causes InvalidOperationException after upgrading to EF Core 6.0
After upgrading Microsoft.AspNetCore.Identity.EntityFrameworkCore , Microsoft.EntityFrameworkCore.Tools and Npgsql.EntityFrameworkCore.PostgreSQL nugget packages to version 6.0.0 , my .NET 6.0 Web API started throwing this exception:将Microsoft.AspNetCore.Identity.EntityFrameworkCore 、 Microsoft.EntityFrameworkCore.Tools和Npgsql.EntityFrameworkCore.PostgreSQL金块包升级到版本6.0.0后,我的 .NET 6.0 Web 8810005121338 开始抛出此异常:
The entity type 'RefreshToken' has been marked as owned and must be referenced from another entity type via a navigation.实体类型“RefreshToken”已标记为拥有,必须通过导航从另一个实体类型引用。 Add a navigation to an entity type that points at 'RefreshToken' or don't configure it as owned.向指向“RefreshToken”的实体类型添加导航,或者不将其配置为拥有。
The exception seems to be thrown whenever executing any operation that involves the database.每当执行任何涉及数据库的操作时,似乎都会抛出异常。 If I understand the error correctly, it does not find the navigation in RefreshToken entity configuration, however my automatically generated ModelSnapshot does seem to contain it.如果我正确理解错误,它不会在 RefreshToken 实体配置中找到导航,但是我自动生成的 ModelSnapshot 似乎确实包含它。
RefreshToken.cs (some properties were removed for simplification purposes) RefreshToken.cs(为简化目的删除了一些属性)
[Owned]
public class RefreshToken
{
[Key]
public int Id { get; set; }
public string Token { get; set; }
public virtual User User { get; set; }
}
User.cs用户.cs
public class User : IdentityUser
{
public List<RefreshToken> RefreshTokens { get; set; }
}
Excerpt from BuildModel(ModelBuilder modelBuilder)摘自BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("API.Models.User", b =>
{
b.OwnsMany("API.Models.RefreshToken", "RefreshTokens", b1 =>
{
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b1.Property<string>("Token")
.HasColumnType("text");
b1.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");
b1.HasKey("Id");
b1.HasIndex("UserId");
b1.ToTable("RefreshTokens");
b1.WithOwner()
.HasForeignKey("UserId");
});
b.Navigation("RefreshTokens");
});
I've looked through EF Core 6.0 Breaking Changes ( https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#owned-index ) but could not find what would cause this issue.我查看了 EF Core 6.0 重大更改( https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#owned-index )但是找不到会导致此问题的原因。
I got a reply on that github thread I linked to , which contains a fix for me that works.我在链接到的 github 线程上收到了回复,其中包含对我有效的修复程序。 In my case I do not use the Attributes, so you might have to switch to the fluent API.就我而言,我不使用属性,因此您可能必须切换到流利的 API。
The thread also has a reference to a 7.0.0 milestone, so that might be worth checking out if you are from the future.该线程还引用了 7.0.0 里程碑,因此如果您来自未来,可能值得一试。
modelBuilder.Entity<DerivedDerivedClass>().OwnsOne(d => d.OwnedType);
modelBuilder.Entity<DerivedClass>().OwnsOne(d => d.OwnedType);
As a workaround avoid calling modelBuilder.Entity().OwnsOne(d => d.OwnedType);解决方法是避免调用 modelBuilder.Entity().OwnsOne(d => d.OwnedType);
As I understand it, you only want to configure it ONCE on the derived class, and not configure it again on the DerivedDerivedClass
.据我了解,您只想在派生的 class 上配置一次,而不是在DerivedDerivedClass
上再次配置它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.