Entity Framework Core - Code First - Two Foreign Keys - One Table

.net-core c# entity-framework entity-framework-core

Question

I am using EF Core. I want my base class to keep reference of CreateByUser, and LastModifiedByUser. Unfortunately I can only get it to work with one foreign key. When I try to add a second foreign key I get this error:

Introducing FOREIGN KEY constraint 'FK_Assignments_Users_LastModifiedByUserId' on table 'Assignments' 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. Note that I can use "Add-Migration" with out any errors, but once I run "Update-Database" it errors.

Below is the relevant code to the problem and error.

public abstract class DivBase
{
    [Key]
    public int Id { get; set; }

    [Required]
    public DateTimeOffset DateCreated { get; set; }

    public int CreatedByUserId { get; set; }

    [Required]
    public DateTimeOffset LastDateModified { get; set; }

    public int LastModifiedByUserId { get; set; }

    [ForeignKey("CreatedByUserId")]
    public DivUser CreatedByUser { get; set; }

    [ForeignKey("LastModifiedByUserId")]
    public DivUser LastModifiedByUser { get; set; }
}

public class DivUser
{
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(256)]
    public string Name { get; set; }

    [Required]
    [MaxLength(256)]
    public string Email { get; set; }
}

public class DivAssignment : DivBase, IDivEvent
{
    public DateTimeOffset StartDate { get; set; }
    public DateTimeOffset EndDate { get; set; }      
}

public interface IDivEvent
{
    [Required]
    public DateTimeOffset StartDate { get; set; }

    [Required]
    public DateTimeOffset EndDate { get; set; }
}

Also, If I make one of the properties nullable it will compile, but I fear that will cause problems later.

1
1
1/24/2020 8:13:30 PM

Accepted Answer

Apparently you have a 2 Fks to the same table in the same row so imagine if you delete a user who has created an assignment and Modified one what should happen, delete the assignment what if other users modified it this will be nulls in heir tables.

so

You need to specify the CascadeOnDelete to false using FluentApi

In the ApplicationContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DivAssignment>()
                .HasRequired(c => c.CreatedByUser)
                .WithMany(u => u.CreatedDivAssignments)
                .HasForeignKey(c => c.CreatedByUserId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<DivAssignment>()
                .HasRequired(c => c.LastModifiedByUser)
                .WithMany(u => u.ModifiedDivAssignments)
                .HasForeignKey(c => c.LastModifiedByUserId)
                .WillCascadeOnDelete(false);
        }

Then add-migration then update-database

0
1/24/2020 9:07:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow