I'm having some issues creating foreign key relationships between my tables using the C# Entity Framework. I have these tables:
CREATE TABLE [dbo].[Course] (
[Id] INT IDENTITY (1, 1) NOT NULL,,
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED ([ID] ASC)
);
CREATE TABLE [dbo].[PreqEdge] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[ParentID] INT NOT NULL,
[ChildID] INT NOT NULL,
CONSTRAINT [PK_PreqEdge] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Dependant] FOREIGN KEY ([ParentID]) REFERENCES [dbo].[Course] ([Id]),
CONSTRAINT [FK_Depends] FOREIGN KEY ([ChildID]) REFERENCES [dbo].[Course] ([Id])
);
The corresponding models look like so:
public partial class Course
{
public int Id { get; set; }
public virtual ICollection<PreqEdge> Parents { get; set; }
public virtual ICollection<PreqEdge> Children { get; set; }
public Course()
{
Parents = new HashSet<PreqEdge>();
Children = new HashSet<PreqEdge>();
}
}
public partial class PreqEdge
{
public int Id { get; set; }
public int ParentID { get; set; }
public int ChildID { get; set; }
public virtual Course Parent { get; set; }
public virtual Course Child { get; set; }
}
Whenever I use my database context to access them I get this error:
Introducing FOREIGN KEY constraint 'FK_dbo.PreqEdges_dbo.Courses_ChildID' on table 'PreqEdges' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
The purpose of these tables is to connect two Courses together like the edge of a graph in order to create a chain of prerequisites for that course that can be traversed either forwards or backwards based on whether you filter by ChildID or ParentID.
I understand that Entity Framework by default uses cascade deletes on references and that I might be able to solve this by overriding the OnModelCreating method in my DbContext, but I'm not sure how to specify using a DbModelBuilder this kind of relationship where it could be foreign keyed with a table by either one of two keys in that table.
Is this possible to do in the Entity Framework without manually writing SQL calls?
EDIT: Changed DependantID and DependsID to ParentID and ChildID for clarity