I have a following problem. I have two entities for which I want to create many to many relationship.
public class Subsystem
{
public int SubsystemId { get; set; }
public string SubsystemName { get; set; }
public virtual ICollection<Component> Components { get; set; }
}
public class Component
{
public int ComponentId { get; set; }
public string ComponentName { get; set; }
public virtual ICollection<Subsystem> Subsystems { get; set; }
}
None of them is more important than the other - they can exist separately, but sometimes they can be connected via an appropriate entry in the join table. Now, after I create a migration, I get something like this:
public override void Up()
{
CreateTable(
"dbo.Component",
c => new
{
ComponentId = c.Int(nullable: false, identity: true),
ComponentName = c.String(nullable: false, maxLength: 100)
})
.PrimaryKey(t => t.ComponentId)
CreateTable(
"dbo.Subsystem",
c => new
{
SubsystemId = c.Int(nullable: false, identity: true),
SubsystemName = c.String(nullable: false, maxLength: 100),
})
.PrimaryKey(t => t.SubsystemId);
CreateTable(
"dbo.SubsystemComponents",
c => new
{
Subsystem_SubsystemId = c.Int(nullable: false),
Component_ComponentId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Subsystem_SubsystemId, t.Component_ComponentId })
.ForeignKey("dbo.Subsystem", t => t.Subsystem_SubsystemId, cascadeDelete: true)
.ForeignKey("dbo.Component", t => t.Component_ComponentId, cascadeDelete: true)
.Index(t => t.Subsystem_SubsystemId)
.Index(t => t.Component_ComponentId);
}
I cannot set cascadeDelete to true, and that is why I keep getting the following error when I update-database:
Introducing FOREIGN KEY constraint 'FK_dbo.SubsystemComponents_dbo.Component_Component_ComponentId' on table 'SubsystemComponents' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I am almost sure that this is the result of cascadeDelete : true.
I don't know how to force it to be set to false. I am using FluentAPI, and everything has to be configured in Configuration class for each entity.