实体与许多一对多的关系创建升级到RC失败后, [英] Entity with many-to-many relationship creation fails after upgrading to RC
问题描述
我有3个简单的表项目,一对夫妇POCO类,并与代码,没有EDML文件创建的DbContext。用下面的代码设置与实体框架代码优先,我编辑的DbContext代码的测试工作,因为模型构建器从公测改为RC
I've got a project with 3 simple tables, a couple of POCO classes, and a DBContext created with code, no edml file. The following code setup used to work with the beta of Entity Framework code-first, I've edited the DbContext code since the ModelBuilder changed from the beta to RC
表(简单的表格很多一对多表有声明为级联删除外键)字段:
Tables (simple tables many-to-many table has fields declared as foreign keys with cascade deletion):
CREATE TABLE [dbo].[Bookings](
[ID] [int] IDENTITY(1,1) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[AccountId] [varchar](50) NOT NULL,
CONSTRAINT [PK_Bookings] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
CREATE TABLE [dbo].[Units](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Description] [nvarchar](1000) NULL,
[Beds] [int] NOT NULL,
CONSTRAINT [PK_Units] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
CREATE TABLE [dbo].[UnitBookings](
[UnitId] [int] NOT NULL,
[BookingId] [int] NOT NULL
) ON [PRIMARY]
POCOS:
public class Unit
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public int Beds { get; set; }
public ICollection<Booking> Bookings { get; set; }
}
public class Booking
{
[Key]
public int ID { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
[Required]
public string AccountId { get; set; }
public ICollection<Unit> Units { get; set; }
}
DB上下文
DB Context
public class BookingDb : DbContext
{
public DbSet<Booking> Bookings { get; set; }
public DbSet<Unit> Units { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Unit>()
.HasMany(u => u.Bookings)
.WithMany(b => b.Units)
.Map(m => m.MapLeftKey("BookingId").MapRightKey("UnitId").ToTable("UnitBookings"));
}
}
创建的预订(该bookingCarrrier是提供一个辅助类由前端层)
Creating a Booking (the bookingCarrrier is a helper class delivered by frontend layer)
public static bool CreateBooking(BookingCarrier carrier, out string statusMsg)
{
using (var db = new BookingDB())
{
var validator = new BookingValidator(2, 3);
Booking booking = CreateBooking(carrier, db);
if (validator.Validate(booking.AccountId, booking, -1, out statusMsg)
{
db.Bookings.Add(booking);
db.SaveChanges();
return true;
}
return false;
}
}
private static CreateBooking(BookingCarrier carrier, BookingDB db)
{
var units = new List<Unit>();
if (carrier.SelectedUnit == 0)
units.AddRange(db.Units.ToList());
else
units.Add(db.Units.Find(carrier.SelectedUnit));
return new Booking
{
AccountId = carrier.AccountId,
EndDate = carrier.EndDate,
StartDate = carrier.StartDate,
Units = units
};
}
在执行这段代码,EF抛出具有以下消息的SQLException:
When executing this code, EF throws a SqlException with the following message:
INSERT语句冲突与
外键约束
FK_UnitBookings_Units。冲突
发生于数据库grashult.dk,
表dbo.Units,列ID。
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UnitBookings_Units". The conflict occurred in database "grashult.dk", table "dbo.Units", column 'ID'.
这是不是在测试的情况。
Which wasn't the case in the beta.
我知道这是一个相当简单的设置,但由于这是一个爱好项目,我想它怎么看简单的像这样的事情可以做的。
I know this is a fairly simplistic setup, but since this a hobby project, and I'm trying it out to see how simple a thing like this can be done.
是谁认识的变化导致此行为?是这样仍然可以使用EF 4.1,或者我必须马上拿出数据模型设计?
Are anybody aware of changes that causes this behaviour? Is something like this still possible with EF 4.1, or do I have to whip out the data model designer?
问候
加斯帕豪格
Regards Jesper Hauge
推荐答案
您必须交换密钥映射:
modelBuilder.Entity<Unit>()
.HasMany(u => u.Bookings)
.WithMany(b => b.Units)
.Map(m => m.MapLeftKey("UnitId")
.MapRightKey("BookingId")
.ToTable("UnitBookings"));
我只是测试它,问题是, Booking.Id
保存在 UnitId
和 UnitId
在 Booking.Id
。
这篇关于实体与许多一对多的关系创建升级到RC失败后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!