0

I'm developing an ASP.NET Core app with Razor pages. I have two tables:

public class User {

    public int Id { get; set; }

    [InverseProperty("Creator")]
    public virtual ICollection<Project> CreatedProjects { get; set; }

    [InverseProperty("ModifiedBy")]
    public virtual ICollection<Project> ModifiedProjects { get; set; }
}

public class Project {

    public int Id { get; set; }

    public int? CreatorId { get; set; }

    [ForeignKey("CreatorId")]
    public virtual User Creator { get; set; }

    public int? ModifiedById { get; set; }

    [ForeignKey("ModifiedById")]
    public virtual User ModifiedBy { get; set; }

}

The above code mostly follows this: Entity Framework Code First - two Foreign Keys from same table

The problem is when i try to remove a User who is created and modified a Project i get the following runtime exception:

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Projects_Users_CreatorId". The conflict occurred in database "DBName", table "dbo.Projects", column 'CreatorId'. The statement has been terminated

I tried to add the following code to OnModelCreating but i got the same error.

modelBuilder.Entity<Project>(entity => {

    entity.HasOne(d => d.Creator)
        .WithMany(p => p.CreatedProjects)
        .HasForeignKey(d => d.CreatorId)
        .OnDelete(DeleteBehavior.ClientSetNull);

    entity.HasOne(d => d.ModifiedBy)
        .WithMany(p => p.ModifiedProjects)
        .HasForeignKey(d => d.ModifiedById)
        .OnDelete(DeleteBehavior.ClientSetNull);

});

After that i tried this:

modelBuilder.Entity<Project>(entity => {

    entity.HasOne(d => d.Creator)
        .WithMany(p => p.CreatedProjects)
        .HasForeignKey(d => d.CreatorId)
        .OnDelete(DeleteBehavior.SetNull);

});

And it worked(after creating new migration and updating the database). I was able to delete a user and the Creator was set to null in the Project as expected. (Of course the modifier was not the same person this time in order to eliminate the first error).

BUT when i tried to "SetNull" BOTH properties like this:

modelBuilder.Entity<Project>(entity => {

    entity.HasOne(d => d.Creator)
        .WithMany(p => p.CreatedProjects)
        .HasForeignKey(d => d.CreatorId)
        .OnDelete(DeleteBehavior.SetNull);

    entity.HasOne(d => d.ModifiedBy)
        .WithMany(p => p.ModifiedProjects)
        .HasForeignKey(d => d.ModifiedById)
        .OnDelete(DeleteBehavior.SetNull);

});

And i created a new migration (Add-Migration ....) i wasn't even able to "Update-Database" because i got the following error: Introducing FOREIGN KEY constraint 'FK_Projects_Users_ModifiedById' on table 'Projects' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

Any ideas how to solve this. How could i delete a user who is referenced in a project as a Creator and a Modifier as well?

Thank You!

0 Answers0