'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:

The entity type 'RefreshToken' has been marked as owned and must be referenced from another entity type via a navigation. Add a navigation to an entity type that points at 'RefreshToken' or don't configure it as owned.

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.cs (some properties were removed for simplification purposes)

[Owned]
public class RefreshToken
{
    [Key]
    public int Id { get; set; }
    public string Token { get; set; }
    public virtual User User { get; set; }
}

User.cs

public class User : IdentityUser
{
    public List<RefreshToken> RefreshTokens { get; set; }
}

Excerpt from 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://docs.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.



Solution 1:[1]

I got a reply on that github thread I linked to, which contains a fix for me that works. In my case I do not use the Attributes, so you might have to switch to the fluent 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.


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


As I understand it, you only want to configure it ONCE on the derived class, and not configure it again on the DerivedDerivedClass.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S. ten Brinke