Skip to content

Adding IdentityUserClaim navigation property to ApplicationUser causes migration to create new FK #58147

@mohaaron

Description

@mohaaron

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am following the directions for adding a navigation property from ApplicationUser to IdentityUserClaims and keep getting a migration from it that is not empty as these directions state it should be. Am I doing something wrong that's causing changes to the migration?

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-8.0#add-navigation-properties

When I add the Claims navigation property to ApplicationUser the migration creates the following.

public class ApplicationUser : IdentityUser
{
    public required string FirstName { get; set; }
    public required string LastName { get; set; }

    public ICollection<IdentityUserClaim<string>> Claims { get; set; } = new List<IdentityUserClaim<string>>();
}
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "ApplicationUserId",
        table: "AspNetUserClaims",
        type: "TEXT",
        nullable: true);

    migrationBuilder.CreateIndex(
        name: "IX_AspNetUserClaims_ApplicationUserId",
        table: "AspNetUserClaims",
        column: "ApplicationUserId");

    migrationBuilder.AddForeignKey(
        name: "FK_AspNetUserClaims_AspNetUsers_ApplicationUserId",
        table: "AspNetUserClaims",
        column: "ApplicationUserId",
        principalTable: "AspNetUsers",
        principalColumn: "Id");
}

I have also tried adding the following configuration for ApplicationUser but it makes no difference.

public partial class ApplicationUserConfiguration : IEntityTypeConfiguration<ApplicationUser>
{
    public void Configure(EntityTypeBuilder<ApplicationUser> builder)
    {
        builder.Property(p => p.AccountOwnerId);

        builder.HasMany(e => e.Claims)
                .WithOne()
                .HasForeignKey(uc => uc.UserId)
                .IsRequired();
    }
}

Expected Behavior

The migration created after adding the Claims navigation property is empty as stated in the directions.

Steps To Reproduce

Here is a sample where I have reproduced the problem.
https://github.com/mohaaron/AddClaimsNavigationToAppUserBug

Exceptions (if any)

No response

.NET Version

8.07

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions