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

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

问题描述

对于使用 Code First EF 5 beta 的应用程序,我有:

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 被删除,它的 ChildObjects 也会被删除.

So if a ParentObject is deleted, its ChildObjects are too.

但是,如果我运行:

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

我得到了异常:

操作失败:无法更改关系,因为一个或多个外键属性不可为空.当对关系进行更改时,相关的外键属性将设置为空值.如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象.

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(在本例中使用 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).

推荐答案

它实际上是支持的,但只有当你使用 识别关系.它也适用于代码.您只需要为包含 IdParentObjectIdChildObject 定义复杂的键:

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天全站免登陆