升级到 RC 后,创建多对多关系的实体失败
我有一个包含 3 个简单表、几个 POCO 类和一个用代码创建的 DBContext 的项目,没有 edml 文件。以下代码设置用于使用实体框架代码优先的测试版,自从模型构建器从测试版更改为 RC
表(简单表多对多表的字段声明为外键) 以来,我编辑了 DbContext 代码级联删除):
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 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"));
}
}
创建 Booking (bookingCarrrier 是前端层提供的辅助类)
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 并显示以下消息:
INSERT 语句与 外键约束 “FK_UnitBookings_Units”。冲突 发生在数据库“grashult.dk”中, 表“dbo.Units”,列“ID”。
测试版中情况并非如此。
我知道这是一个相当简单的设置,但由于这是一个业余爱好项目,我正在尝试看看这样的事情可以多么简单地完成。
有人知道导致这种行为的变化吗? EF 4.1 仍然可以实现类似的功能吗?还是我必须去掉数据模型设计器?
问候 杰斯帕·豪格
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 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"));
}
}
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
};
}
When executing this code, EF throws a SqlException with the following message:
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须交换键映射:
我刚刚测试了它,问题是
Booking.Id
存储在UnitId
中,而UnitId
存储在中预订.Id
。You must swap your key mapping:
I just tested it and the problem was that
Booking.Id
was stored inUnitId
andUnitId
inBooking.Id
.