Adding navigation properties EF Core .NET Core 3.1 - CREATE UNIQUE INDEX Error

294 Views Asked by At

I have the following two tables:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...
    public ApplicationUser RegisteredPerson { get; set; }        
}

public class ApplicationUser : IdentityUser
{
    public DateTime DateJoined { get; set; }
    public int PersonId { get; set; }
    public string DisplayName { get; set; }
    ...
    public Person Person { get; set; }        
}

When creating a new migration to add the navigation properties, the following migration code is produced:

migrationBuilder.CreateIndex(
    name: "IX_AspNetUsers_PersonId",
    table: "AspNetUsers",
    column: "PersonId",
    unique: true);

migrationBuilder.AddForeignKey(
    name: "FK_AspNetUsers_Persons_PersonId",
    table: "AspNetUsers",
    column: "PersonId",
    principalTable: "Persons",
    principalColumn: "Id",
    onDelete: ReferentialAction.Cascade);

I get the following error when trying to update the database with the new migration

CREATE UNIQUE INDEX [IX_AspNetUsers_PersonId] ON [AspNetUsers] ([PersonId]);

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.AspNetUsers' and the index name 'IX_AspNetUsers_PersonId'. The duplicate key value is (0).

Suggestions for fix appreciated.

Thanks

0

There are 0 best solutions below