C#C
C#16mo ago
Saiyanslayer

✅ EntityFramework Core trying to add Navigation Property that already exists

I'm getting this fault:
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'UNIQUE constraint failed: AspNetUsers.NormalizedUserName'.


Entity class:
public class PlanEntity : ISortable {
    public int Id { get; set; }
    public int SortPriority { get; set; }
    public string Label { get; set; }
    public List<TaskSetEntity> TaskSets { get; set; } = [];

    public DateTime? StartDate { get; set; }
    public List<ApplicationUser> Owners { get; set; } = [];
    public string[] Sites { get; set; } = [];
}


The owners property linked:

public class ApplicationUser : IdentityUser {
    [Required, MaxLength(55)]
    public string Name { get; set; }

}


The model builder:

public static partial class ModelBuilderExtensions {
    public static ModelBuilder AddPlanEntities(this ModelBuilder modelBuilder) {
        modelBuilder.Entity<PlanEntity>()
            .ToTable("RefactoredPlans")
            .HasMany(x => x.TaskSets)
            .WithOne()
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<PlanEntity>()
            .ToTable("RefactoredPlans")
            .HasMany(x => x.Owners)
            .WithMany();

        return modelBuilder;
    }
}
Was this page helpful?