实体框架代码第一:循环或多个级联路径 [英] Entity Framework Code first: cycles or multiple cascade paths

查看:89
本文介绍了实体框架代码第一:循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有预订联系人(一个 Person )和一组导航属性( People )通过连接表链接到 Person 中的另一组导航属性(预订)。如何为预订联系人关系生成启用级联删除的预订表?当我离开流畅的API代码(启用级联删除的默认设置)时,我从迁移中收到以下错误消息:

I have a Booking class that has a booking contact (a Person) and a set of navigation properties (People) that links through a join table to another set of navigation properties (Bookings) in Person. How do I generate the Booking table with cascading deletes enabled for the booking contact relationship? When I leave it out of the fluent API code (default setting of cascade delete enabled) I get the following error message from migration:


介绍FOREIGN KEY约束
'FK_dbo.BookingPeople_dbo.People_PersonID'在表'BookingPeople'
可能会导致循环或多个级联路径。指定ON DELETE NO
ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY
约束。

Introducing FOREIGN KEY constraint 'FK_dbo.BookingPeople_dbo.People_PersonID' on table 'BookingPeople' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

无法创建约束或索引。查看以前的错误。

Could not create constraint or index. See previous errors.



 modelBuilder.Entity<Person>()
   .HasMany<Booking>(s => s.aBookings)
   .WithRequired(s => s.Contact)
   .HasForeignKey(s => s.ContactId); 


 modelBuilder.Entity<Booking>()
   .HasMany(t => t.People)
   .WithMany(t => t.Bookings)
   .Map(m => {
     m.ToTable("BookingPeople");
     m.MapLeftKey("BookingID");
     m.MapRightKey("PersonID");
   });


推荐答案

问题是您有多条级联删除路径可能会尝试删除DB中 BookingPeople 表中的同一行。

The problem is you have multiple paths of cascade deletes that could end trying to delete the same row in the BookingPeople table in DB.

您可以避免这种模糊的删除路径通过使用 Fluent API :

You can avoid such ambiguous delete paths by either disabling cascading delete in the one-to-many relationship using Fluent API:

    modelBuilder.Entity<Booking>()
                .HasRequired(s => s.Contact)
                .WithMany(s => s.aBookings)
                .HasForeignKey(s => s.ContactId)
                .WillCascadeOnDelete(false);

或者通过将关系定义为可选(使用可空的外键,但不能配置关系使用Fluent Api进行级联删除)。

Or by defining the relationship as optional (with a nullable foreign key, but you can not configure the relationship with cascade delete using Fluent Api).

     modelBuilder.Entity<Booking>()
            .HasOptional(s => s.Contact)
            .WithMany(s => s.aBookings)
            .HasForeignKey(s => s.ContactId);// ContactId is a nullable FK property

另外,您可以使用以下方式删除级联删除约定:

Also, you can remove the cascade delete convention by using:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

或者在多对多关系的情况下:

Or in the case of the many-to-many relationship:

modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

如果您需要删除所有的预订当您删除它时,与 Person 相关联,我的建议是将一对多关系配置为可选,并覆盖 SaveChanges 方法:

If you need to delete all the Bookings asociated with a Person when you delete it, my advice is configure the one-to-many relationship as optional, and override the SaveChanges method:

public override int SaveChanges()
{
    Bookings.Local
            .Where(r => r.ContactId == null)
            .ToList()
            .ForEach(r => Bookings.Remove(r));

    return base.SaveChanges();
 }

如果依赖实体上的外键为空,则Code First不设置对关系进行级联删除,当主体被删除时,外键将被设置为 null 。这样,您可以在 SaveChanges 方法中找到孤儿并将其删除

If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null. This way, you can find the orphans in the SaveChanges method and delete them

这篇关于实体框架代码第一:循环或多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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