© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
34 replies
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'.
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; } = [];
}
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; }

}
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;
    }
}
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;
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EntityFramework Property Binding
C#CC# / help
3y ago
✅ EntityFramework: Navigation property makes a nested object call
C#CC# / help
3y ago
❔ EF blowing up trying to insert a relationship that already exists
C#CC# / help
3y ago