【发布时间】:2015-03-14 09:46:57
【问题描述】:
尝试创建代码优先多对多关系时显示以下错误。有人可以告诉我有什么问题吗?我到处搜索,看不出代码有什么问题。
在表“BookingPeople”上引入 FOREIGN KEY 约束“FK_dbo.BookingPeople_dbo.People_PersonID”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束或索引。查看以前的错误。
迁移:
public override void Up()
{
CreateTable(
"dbo.Bookings",
c => new
{
BookingID = c.Int(nullable: false),
startDate = c.DateTime(nullable: false),
endDate = c.DateTime(nullable: false),
Contact_PersonID = c.Int(nullable: false),
Type_BookingTypeID = c.Int(nullable: false),
})
.PrimaryKey(t => t.BookingID)
.ForeignKey("dbo.People", t => t.Contact_PersonID, cascadeDelete: true)
.ForeignKey("dbo.BookingTypes", t => t.Type_BookingTypeID, cascadeDelete: true)
.Index(t => t.Contact_PersonID)
.Index(t => t.Type_BookingTypeID);
CreateTable(
"dbo.People",
c => new
{
PersonID = c.Int(nullable: false),
PersonAge = c.Int(nullable: false),
Type_PersonTypeID = c.Int(nullable: false),
})
.PrimaryKey(t => t.PersonID)
.ForeignKey("dbo.PersonTypes", t => t.Type_PersonTypeID, cascadeDelete: true)
.Index(t => t.Type_PersonTypeID);
CreateTable(
"dbo.PersonTypes",
c => new
{
PersonTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.PersonTypeID);
CreateTable(
"dbo.BookingTypes",
c => new
{
BookingTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.BookingTypeID);
CreateTable(
"dbo.Users",
c => new
{
UserID = c.Int(nullable: false),
Username = c.String(maxLength: 30),
Password = c.String(unicode: false),
})
.PrimaryKey(t => t.UserID);
CreateTable(
"dbo.BookingPeople",
c => new
{
BookingID = c.Int(nullable: false),
PersonID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.BookingID, t.PersonID })
.ForeignKey("dbo.Bookings", t => t.BookingID, cascadeDelete: true)
.ForeignKey("dbo.People", t => t.PersonID, cascadeDelete: true)
.Index(t => t.BookingID)
.Index(t => t.PersonID);
}
流畅的 API 代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(e => e.Password)
.IsUnicode(false);
modelBuilder.Entity<Booking>()
.HasMany(t => t.People)
.WithMany(t => t.Bookings)
.Map(m =>
{
m.ToTable("BookingPeople");
m.MapLeftKey("BookingID");
m.MapRightKey("PersonID");
});
}
预订舱位:
public class Booking
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int BookingID { get; set; }
[Required]
public BookingType Type { get; set; }
[Required]
public Person Contact { get; set; }
[Required]
public DateTime startDate { get; set; }
[Required]
public DateTime endDate { get; set; }
public virtual ICollection<Person> People { get; set; }
}
人物类:
public class Person
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int PersonID { get; set; }
[Required]
public int PersonAge { get; set; }
[Required]
public PersonType Type { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
更新代码
迁移:
public override void Up()
{
CreateTable(
"dbo.Bookings",
c => new
{
BookingID = c.Int(nullable: false),
startDate = c.DateTime(nullable: false),
endDate = c.DateTime(nullable: false),
BookingType_BookingTypeID = c.Int(),
})
.PrimaryKey(t => t.BookingID)
.ForeignKey("dbo.People", t => t.BookingID)
.ForeignKey("dbo.BookingTypes", t => t.BookingType_BookingTypeID)
.ForeignKey("dbo.BookingTypes", t => t.BookingID)
.Index(t => t.BookingID)
.Index(t => t.BookingType_BookingTypeID);
CreateTable(
"dbo.People",
c => new
{
PersonID = c.Int(nullable: false),
PersonAge = c.Int(nullable: false),
PersonType_PersonTypeID = c.Int(),
})
.PrimaryKey(t => t.PersonID)
.ForeignKey("dbo.PersonTypes", t => t.PersonType_PersonTypeID)
.ForeignKey("dbo.PersonTypes", t => t.PersonID)
.Index(t => t.PersonID)
.Index(t => t.PersonType_PersonTypeID);
CreateTable(
"dbo.PersonTypes",
c => new
{
PersonTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.PersonTypeID);
CreateTable(
"dbo.BookingTypes",
c => new
{
BookingTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.BookingTypeID);
CreateTable(
"dbo.Users",
c => new
{
UserID = c.Int(nullable: false, identity: true),
Username = c.String(maxLength: 30),
Password = c.String(unicode: false),
})
.PrimaryKey(t => t.UserID);
CreateTable(
"dbo.BookingPeople",
c => new
{
BookingID = c.Int(nullable: false),
PersonID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.BookingID, t.PersonID })
.ForeignKey("dbo.Bookings", t => t.BookingID, cascadeDelete: true)
.ForeignKey("dbo.People", t => t.PersonID, cascadeDelete: true)
.Index(t => t.BookingID)
.Index(t => t.PersonID);
}
预订:
public partial class Booking
{
public int BookingID { get; set; }
public BookingType Type { get; set; }
public Person Contact { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public virtual ICollection<Person> People { get; set; }
}
人:
public partial class Person
{
public int PersonID { get; set; }
public int PersonAge { get; set; }
public PersonType Type { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
流畅的 API 代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(e => e.Password)
.IsUnicode(false);
// Booking
// -------
modelBuilder.Entity<Booking>()
.HasRequired(b => b.Contact)
.WithOptional()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Booking>()
.HasRequired(b => b.Type)
.WithOptional()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Booking>()
.HasMany(t => t.People)
.WithMany(t => t.Bookings)
.Map(m =>
{
m.ToTable("BookingPeople");
m.MapLeftKey("BookingID");
m.MapRightKey("PersonID");
});
// Person
// ------
modelBuilder.Entity<Person>()
.HasRequired(b => b.Type)
.WithOptional()
.WillCascadeOnDelete(false);
}
更新代码:
迁移:
public override void Up()
{
CreateTable(
"dbo.Bookings",
c => new
{
BookingID = c.Int(nullable: false, identity: true),
startDate = c.DateTime(nullable: false),
endDate = c.DateTime(nullable: false),
Contact_PersonID = c.Int(nullable: false),
BookingType_BookingTypeID = c.Int(),
Type_BookingTypeID = c.Int(nullable: false),
})
.PrimaryKey(t => t.BookingID)
.ForeignKey("dbo.People", t => t.Contact_PersonID)
.ForeignKey("dbo.BookingTypes", t => t.BookingType_BookingTypeID)
.ForeignKey("dbo.BookingTypes", t => t.Type_BookingTypeID)
.Index(t => t.Contact_PersonID)
.Index(t => t.BookingType_BookingTypeID)
.Index(t => t.Type_BookingTypeID);
CreateTable(
"dbo.People",
c => new
{
PersonID = c.Int(nullable: false, identity: true),
PersonAge = c.Int(nullable: false),
PersonType_PersonTypeID = c.Int(),
Type_PersonTypeID = c.Int(nullable: false),
})
.PrimaryKey(t => t.PersonID)
.ForeignKey("dbo.PersonTypes", t => t.PersonType_PersonTypeID)
.ForeignKey("dbo.PersonTypes", t => t.Type_PersonTypeID)
.Index(t => t.PersonType_PersonTypeID)
.Index(t => t.Type_PersonTypeID);
CreateTable(
"dbo.PersonTypes",
c => new
{
PersonTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.PersonTypeID);
CreateTable(
"dbo.BookingTypes",
c => new
{
BookingTypeID = c.Int(nullable: false, identity: true),
Type = c.String(),
})
.PrimaryKey(t => t.BookingTypeID);
CreateTable(
"dbo.Users",
c => new
{
UserID = c.Int(nullable: false, identity: true),
Username = c.String(maxLength: 30),
Password = c.String(unicode: false),
})
.PrimaryKey(t => t.UserID);
CreateTable(
"dbo.BookingPeople",
c => new
{
BookingID = c.Int(nullable: false),
PersonID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.BookingID, t.PersonID })
.ForeignKey("dbo.Bookings", t => t.BookingID, cascadeDelete: true)
.ForeignKey("dbo.People", t => t.PersonID, cascadeDelete: true)
.Index(t => t.BookingID)
.Index(t => t.PersonID);
}
流畅的 API 代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(e => e.Password)
.IsUnicode(false);
// Booking
// -------
modelBuilder.Entity<Booking>()
.HasRequired(b => b.Contact)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Booking>()
.HasRequired(b => b.Type)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Booking>()
.HasMany(t => t.People)
.WithMany(t => t.Bookings)
.Map(m =>
{
m.ToTable("BookingPeople");
m.MapLeftKey("BookingID");
m.MapRightKey("PersonID");
});
// Person
// ------
modelBuilder.Entity<Person>()
.HasRequired(b => b.Type)
.WithMany()
.WillCascadeOnDelete(false);
}
【问题讨论】:
标签: c# entity-framework