Multiple Foreign Key for Same table in Entity Framework Code First

Dogan Can Topal

I have two entities in my application and I populated the database with Entity Framework Code First. There are two Employee id in the Task entity; one of them forRequirerEmploye, others for RequestedEmployee. When make update-database in package manager console I get error like this:

Introducing FOREIGN KEY constraint 'FK_dbo.Tasks_dbo.Employees_DemandingEmployeeID' on table 'Tasks' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

My Task Class:

    public int ID { get; set; }
    public int DemandingEmployeeID { get; set; }
    public int RequestedEmployeeID { get; set; }



    [ForeignKey("DemandingEmployeeID")]
    [InverseProperty("DemandingTasks")]
    public virtual Employee DemandingEmployee { get; set; }

    [ForeignKey("RequestedEmployeeID")]
    [InverseProperty("RequestedTasks")]

    public virtual Employee RequestedEmployee { get; set; }

My Employee Class:

    public int ID { get; set; }

    public virtual ICollection<Task> DemandingTasks { get; set; }    
    public virtual ICollection<Task> RequestedTasks { get; set; }

My Context:

   modelBuilder.Entity<Task>().HasRequired(m => m.DemandingEmployee).WithMany(m => m.DemandingTasks).HasForeignKey(m => m.DemandingEmployeeID);
        modelBuilder.Entity<Task>().HasRequired(m => m.UpdatedEmployee).WithMany(m => m.UpdatedTasks).HasForeignKey(m => m.UpdatedEmployeeID);
Fabio Luz

The problem is that you have multiple paths of cascade deletes, that could end trying to delete the same row in the database. To understand more about this error, take a look at https://support.microsoft.com/en-us/kb/321843. Follow the steps above to solve the problem.

Change your class:

public int ID { get; set; }
public int DemandingEmployeeID { get; set; }
public int RequestedEmployeeID { get; set; }

public virtual Employee DemandingEmployee { get; set; }
public virtual Employee RequestedEmployee { get; set; }

Change your context:

modelBuilder.Entity<Task>()
     .HasRequired(m => m.DemandingEmployee)
     .WithMany(m => m.DemandingTasks)
     .HasForeignKey(m => m.DemandingEmployeeID)
     .WillCascadeOnDelete(false);

modelBuilder.Entity<Task>()
     .HasRequired(m => m.RequestedEmployee)
     .WithMany(m => m.RequestedTasks)
     .HasForeignKey(m => m.RequestedEmployeeId)
     .WillCascadeOnDelete(false);

modelBuilder.Entity<Task>()
    .HasRequired(m => m.UpdatedEmployee)
    .WithMany(m => m.UpdatedTasks)
    .HasForeignKey(m => m.UpdatedEmployeeID)
    .WillCascadeOnDelete(false);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Defining multiple Foreign Key for the Same table in Entity Framework Code First

From Dev

Code-first Entity Framework - Multiple Foreign Keys For the Same Table

From Dev

Entity Framework 6 multiple table to one foreign key relationship code first

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Entity Framework Code First Foreign Key issue

From Dev

Foreign Key in Code First Entity Framework

From Dev

Entity Framework: Foreign Key in code first

From Dev

Entity Framework multiple foreign keys to the same table

From Dev

Entity Framework multiple foreign keys to the same table

From Dev

EF Code First Foreign Key Same Table

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths

From Dev

Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

From Dev

FK to the Same Table Code First Entity Framework

From Dev

Entity Framework Code First Foreign Key adding Index as well

From Dev

Entity Framework 6 Code First Foreign Key Without Corresponding Properties

From Dev

Entity Framework Code First and Firebird - Foreign Key name issue

From Dev

Entity Framework Code first adds unwanted foreign key column

From Dev

Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

From Dev

Get foreign key value using Entity Framework code first

From Dev

Entity Framework one to optional foreign key code first fluent mapping

From Dev

Entity framework code first cant create primary and foreign key relationship

From Dev

Entity framework code first, get access to foreign key value

From Dev

Entity framework code first, custom foreign key name for inheritance

From Dev

How to specify a foreign key with code-first Entity Framework

From Dev

Exchanged Foreign Key on Entity Framework Code First From data base

From Dev

Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

From Dev

Enable multiple many to many in Entity Framework 5 code first to same table

From Dev

Code First entity framework and foreign keys

Related Related

  1. 1

    Defining multiple Foreign Key for the Same table in Entity Framework Code First

  2. 2

    Code-first Entity Framework - Multiple Foreign Keys For the Same Table

  3. 3

    Entity Framework 6 multiple table to one foreign key relationship code first

  4. 4

    Entity Framework Code First Foreign Key issue

  5. 5

    Entity Framework Code First Foreign Key issue

  6. 6

    Foreign Key in Code First Entity Framework

  7. 7

    Entity Framework: Foreign Key in code first

  8. 8

    Entity Framework multiple foreign keys to the same table

  9. 9

    Entity Framework multiple foreign keys to the same table

  10. 10

    EF Code First Foreign Key Same Table

  11. 11

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  12. 12

    Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths

  13. 13

    Entity framework 7 in ASP.NET MVC6 multiple foreign key to the same table

  14. 14

    FK to the Same Table Code First Entity Framework

  15. 15

    Entity Framework Code First Foreign Key adding Index as well

  16. 16

    Entity Framework 6 Code First Foreign Key Without Corresponding Properties

  17. 17

    Entity Framework Code First and Firebird - Foreign Key name issue

  18. 18

    Entity Framework Code first adds unwanted foreign key column

  19. 19

    Entity Framework - Code first - Data annotations - Unnecessary foreign key columns

  20. 20

    Get foreign key value using Entity Framework code first

  21. 21

    Entity Framework one to optional foreign key code first fluent mapping

  22. 22

    Entity framework code first cant create primary and foreign key relationship

  23. 23

    Entity framework code first, get access to foreign key value

  24. 24

    Entity framework code first, custom foreign key name for inheritance

  25. 25

    How to specify a foreign key with code-first Entity Framework

  26. 26

    Exchanged Foreign Key on Entity Framework Code First From data base

  27. 27

    Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

  28. 28

    Enable multiple many to many in Entity Framework 5 code first to same table

  29. 29

    Code First entity framework and foreign keys

HotTag

Archive