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.