public class Names
{
[Key]
public int NameID { get; set; }
[StringLength(100)]
[Index(IsUnique = true)]
[Required]
public string Name { get; set; }
}
public class People
{
[Key]
public int PeopleID { get; set; }
[Required]
public int FirstNameID { get; set; }
[ForeignKey("FirstNameID")]
public Names FirstName { get; set; }
public int MiddleNameID { get; set; }
[ForeignKey("MiddleNameID")]
public Names MiddleName { get; set; }
public int LastNameID { get; set; }
[ForeignKey("LastNameID")]
public Names LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
创建迁移并尝试更新数据库后,我收到以下错误:
Introducing FOREIGN KEY constraint 'FK_dbo.People_dbo.Names_LastNameID' on table 'People' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
无法创建约束或索引。查看以前的错误。
根据我在网上看过的其他文章,我在上下文中添加了以下内容,但它并没有按照我的意愿行事。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
以下是生成的迁移代码......
namespace MyDataSet.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class PeopleNameIDs : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Names",
c => new
{
NameID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 100),
})
.PrimaryKey(t => t.NameID)
.Index(t => t.Name, unique: true);
CreateTable(
"dbo.People",
c => new
{
PeopleID = c.Int(nullable: false, identity: true),
FirstNameID = c.Int(nullable: false),
MiddleNameID = c.Int(nullable: false),
LastNameID = c.Int(nullable: false),
DateOfBirth = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.PeopleID)
.ForeignKey("dbo.Names", t => t.FirstNameID, cascadeDelete: true)
.ForeignKey("dbo.Names", t => t.LastNameID, cascadeDelete: true)
.ForeignKey("dbo.Names", t => t.MiddleNameID, cascadeDelete: true)
.Index(t => t.FirstNameID)
.Index(t => t.MiddleNameID)
.Index(t => t.LastNameID);
}
public override void Down()
{
DropForeignKey("dbo.People", "MiddleNameID", "dbo.Names");
DropForeignKey("dbo.People", "LastNameID", "dbo.Names");
DropForeignKey("dbo.People", "FirstNameID", "dbo.Names");
DropIndex("dbo.People", new[] { "LastNameID" });
DropIndex("dbo.People", new[] { "MiddleNameID" });
DropIndex("dbo.People", new[] { "FirstNameID" });
DropIndex("dbo.Names", new[] { "Name" });
DropTable("dbo.People");
DropTable("dbo.Names");
}
}
}
我的目标是拥有一个“People”表,其中所有* NameID都是Names表的外键。但是,只需要FirstName。当我第一次测试它时,我只在FirstNameID上有外键,它运行正常。但是,当我将其复制/粘贴到其他名称时,那就是我现在所处的位置。
感谢任何和所有帮助。非常感谢你。
答案 0 :(得分:0)
只能使用流畅的配置关闭级联删除。但是,你尝试了什么
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
无效,因为您没有many-to-many
关系,但有3 one-to-many
个关系。相反,您应该单独配置每个关系:
modelBuilder.Entity<People>()
.HasRequired(p => p.FirstName)
.WithMany()
.HasForeignKey(p => p.FirstNameID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<People>()
.HasRequired(p => p.MiddleName)
.WithMany()
.HasForeignKey(p => p.MiddleNameID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<People>()
.HasRequired(p => p.LastName)
.WithMany()
.HasForeignKey(p => p.LastNameID)
.WillCascadeOnDelete(false);