我有以下问题。我有两个实体,我想为其建立多对多关系。
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; }
}
没有一个比另一个更重要-它们可以单独存在,但有时可以通过联接表中的适当条目进行连接。现在,在创建迁移后,我得到如下信息:
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);
}
我无法将cascadeDelete设置为true,这就是为什么在更新数据库时始终出现以下错误:
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.
我几乎可以肯定这是级联删除的结果:是。
我不知道如何强制将其设置为false。我正在使用FluentAPI,并且必须在每个实体的Configuration类中进行所有配置。