可以自动EF删除孤立那里的父母没有被删除的数据,? [英] Can EF automatically delete data that is orphaned, where the parent is not deleted?

查看:95
本文介绍了可以自动EF删除孤立那里的父母没有被删除的数据,?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关使用代码首先EF 5测试版我有一个应用程序:

For an application using Code First EF 5 beta I have:

public class ParentObject
{
    public int Id {get; set;}
    public virtual List<ChildObject> ChildObjects {get; set;}
    //Other members
}

public class ChildObject
{
    public int Id {get; set;}
    public int ParentObjectId {get; set;}
    //Other members
}



相关CRUD操作通过资料库,在必要的。

The relevant CRUD operations are performed by repositories, where necessary.

OnModelCreating(DbModelBuilder modelBuilder)

我已经建立起来:

modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects)
            .WithOptional()
            .HasForeignKey(c => c.ParentObjectId)
            .WillCascadeOnDelete();



所以,如果一个 ParentObject 被删除,

However, if I run:

但是,如果我跑.ChildObjects.Clear();
_parentObjectRepository.SaveChanges(); //这个仓库使用上下文

parentObject.ChildObjects.Clear(); _parentObjectRepository.SaveChanges(); //this repository uses the context



我得到异常:

I get the exception:

操作失败:关系不能被改变,因为一个或多个外键的属性是不可为空。当一个变化是有关系提出,相关外键属性设置为空值。如果外键不支持空值,一个新的关系必须定义,外键属性必须指定一个非空值,或者无关的对象必须被删除。

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

这是有道理的作为实体的定义包括外键约束正在​​被打破。

This makes sense as the definition of the entities includes the foreign key constraint which is being broken.

我可以配置实体为自行清除了,当它被孤立或必须我手动从上下文中删除这些 ChildObject S(在这种情况下使用ChildObjectRepository)。

Can I configure the entity to "clear itself up" when it gets orphaned or must I manually remove these ChildObjects from the context (in this case using a ChildObjectRepository).

推荐答案

它实际上是支持的,但只有当你使用的确定关系。它与代码首先为好。你只需要定义复杂的按键为您包含 ChildObject 两者编号 ParentObjectId

It is actually supported but only when you use Identifying relation. It works with code first as well. You just need to define complex key for your ChildObject containing both Id and ParentObjectId:

modelBuilder.Entity<ChildObject>()
            .HasKey(c => new {c.Id, c.ParentObjectId});

由于定义这样的键删除默认约定自动递增的ID,必须手动重新定义它:

Because defining such key will remove default convention for auto incremented Id you must redefine it manually:

modelBuilder.Entity<ChildObject>()
            .Property(c => c.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

现在打电话给parentObject.ChildObjects.Clear()删除相关对象。

Now calling to parentObject.ChildObjects.Clear() deletes dependent objects.

顺便说一句。你的关系映射应该使用 WithRequired 按照你的真正的类,因为如果FK不能为空,这是不可选的:

Btw. your relation mapping should use WithRequired to follow your real classes because if FK is not nullable, it is not optional:

modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects)
            .WithRequired()
            .HasForeignKey(c => c.ParentObjectId)
            .WillCascadeOnDelete();

这篇关于可以自动EF删除孤立那里的父母没有被删除的数据,?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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