简体   繁体   English

升级到 EF Core 6.0 后,拥有的实体导致 InvalidOperationException

[英]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.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.ToolsNpgsql.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.

相关问题 具有多态性 EF 核心 6 的拥有实体 - Owned Entity with Polymorphism EF core 6 更新拥有的实体 EF Core 5 - Update owned entity EF Core 5 EF Core 拥有的实体影子 PK 导致 SQLite 违反空约束 - EF Core owned entity shadow PK causes null constraint violation with SQLite EF core 3.1.1 - 拥有实体代理问题 - EF core 3.1.1 - owned entity proxy problem InvalidOperationException:无法跟踪实体实例 - EF Core - InvalidOperationException: The instance of entity cannot be tracked - EF Core EF Core 6.0 InvalidOperationException:对象已从模型中删除 - EF Core 6.0 InvalidOperationException: The object has been removed from the model 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 EF Core中已修改实体的拥有类型属性不存在 - Owned type property not persisting for a modified entity in EF Core 如何在 ef core 6 中配置引用另一个实体的拥有类型 - How to configure Owned type referencing another entity in ef core 6 EF核心拥有的财产状态传播到上级实体 - EF Core owned property state propagation to parent entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM