I am trying to create a database using EF5 Code First. This is my domain class for the SmartCodeRights table:
public class SmartCodeRight { [Key, Column(Order = 1)] public long UserId { get; set; } public virtual User User { get; set; } [Key, Column(Order = 2)] public long CodeId { get; set; } public virtual SmartCode Code { get; set; } public CodeRight CodeRight { get; set; } }
This table has just been made to facilitate the many-to-many relationship between User and SmartCode .
And this is part of the domain class for my Code table:
public class Code {
CodeRight is enum .
The application of these two restrictions:
ALTER TABLE [dbo].[SmartCodeRights] ADD CONSTRAINT [FK_dbo.SmartCodeRights_dbo.Users_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users] ([UserId]) ON DELETE CASCADE ALTER TABLE [dbo].[SmartCodeRights] ADD CONSTRAINT [FK_dbo.SmartCodeRights_dbo.SmartCodes_CodeId] FOREIGN KEY ([CodeId]) REFERENCES [dbo].[SmartCodes] ([CodeId]) ON DELETE CASCADE
It leads to the following error:
System.Data.SqlClient.SqlException (0x80131904): Entering the FOREIGN KEY constraint FK_dbo.SmartCodeRights_dbo.SmartCodes_CodeId in the SmartCodeRights table can cause loops or multiple cascading paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION or change other FOREIGN KEY constraints. Failed to create constraint. See Previous Errors.
What am I doing wrong and how to do it right? It is preferable to use CodeFirst Data Annotations .
source share