实体框架CTP5,code-第一。多对多使用级联删除 [英] Entity Framework CTP5, code-first. Many to many with cascade delete

查看:236
本文介绍了实体框架CTP5,code-第一。多对多使用级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体(客户和CustomerRole),并想宣布他们之间的许多一对多的关系。我可以做使用下面的code:

I have two entities (Customer and CustomerRole) and would like to declare many-to-many relationship between them. I can do using the following code:

modelBuilder.Entity<CustomerRole>()
        .HasMany(cr => cr.Customers) 
        .WithMany(c => c.CustomerRoles)
        .Map(m => m.ToTable("Customer_CustomerRole_Mapping"));

但它会创建级联关系(和第三映射表),在默认情况下删除关闭。我怎么能告诉EF创建具有级联关系使用多到多少?

But it creates the relationship (and the third mapping table) with cascade delete switched off by default. How can I tell EF to create the relationship with cascade delete switched on when using many-to-many?

推荐答案

由于CTP5的,似乎有没有办法在级联转而直接通过流畅API将删除多对多关联。

As of CTP5, there seems to be no way to directly turn on cascade deletes on Many to Many associations by Fluent API.

也就是说,如果你的目的是无需担心连接表(即Customer_CustomerRole_Mapping)的相关记录,以确保您可以删除主体(如客户记录),那么你就不需要启用级联在客户端删除,当涉及到多对多关联,因为EF code首先对数据库的级联会照顾。

That said, if your intention is to make sure that you can delete the principal (e.g. a Customer record) without having to worry about the dependent record in the join table (i.e. Customer_CustomerRole_Mapping) then you don't need to turn on cascades on the database since EF Code First will take care of the cascade deletes on the client side when it comes to Many to Many associations.

例如,当你删除一个Customer对象,EF是足够聪明,首先发送一个delete语句中的连接表摆脱依赖的纪录之后,它会再派delete语句删除客户记录。

For example, when you delete a Customer object, EF is smart enough to first send a delete statement to get rid of the dependent record in the join table and after that it will send another delete statement to delete the Customer record.

由于CTP5一个bug,你需要明确急于/延迟加载的导航属性并将其装上,当你删除依赖于上下文。例如,考虑这一模式:

Due to a bug in CTP5, you need to explicitly eager/Lazy load the navigation property and have it loaded on the context when you remove the dependent. For example, consider this model:

public class User
{
 public int UserId { get; set; }
 public virtual ICollection Addresses { get; set; }
}

public class Address
{
 public int AddressID { get; set; } 
 public virtual ICollection Users { get; set; }
}

假设我们有一个数据库中的一个地址的用户,这code会抛出:

Assuming that we have a User with an address in the database, this code will throw:

using (EntityMappingContext context = new EntityMappingContext())
{
 User user = context.Users.Find(1); 
 context.Users.Remove(user);
 context.SaveChanges();
}

不过,这人会完全符合先删除链接表的记录工作:

However, this one will perfectly work with removing the link table's record first:

using (EntityMappingContext context = new EntityMappingContext())
{
 User user = context.Users.Find(1); 
((IObjectContextAdapter)context).ObjectContext
                                .LoadProperty(user, u => u.Addresses);
 context.Users.Remove(user);
 context.SaveChanges();
}

请注意,这只是一种变通方法,我们就可以(希望)删除一个校长不加载其导航属性。

Please note that this is just a workaround and we will be able to (hopefully) remove a principal without loading its navigation property.

这篇关于实体框架CTP5,code-第一。多对多使用级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆