简体   繁体   English

EF代码优先+从父级删除子级对象?

[英]EF Code first + Delete Child Object from Parent?

I have a one-to-many relationship between my table Case and my other table CaseReplies . 我的表Case和其他表CaseReplies之间存在一对多关系。 I'm using EF Code First and now wants to delete a CaseReply from a Case object, however it seems impossible to do such thing because it just tries to remove the CaseId from the specific CaseReply record and not the record itself.. 我正在使用EF Code First,现在想从Case对象中删除CaseReply ,但是这样做似乎是不可能的,因为它只是尝试从特定的CaseReply记录而不是记录本身中删除CaseId。

I've tried to set cascade delete/update at database level without luck... 我试图在数据库级别设置级联删除/更新,但是没有运气。

short: Case just removes the relationship between itself and the CaseReply.. it does not delete the CaseReply. 简短说明:Case仅删除其自身与CaseReply之间的关系。它不会删除CaseReply。

My code: 我的代码:

// Case.cs (Case Object) // Case.cs(案例对象)

public class Case
{
    [Key]
    public int Id { get; set; }

    public string Topic { get; set; }

    public string Message { get; set; }

    public DateTime Date { get; set; }

    public Guid UserId { get; set; }

    public virtual User User { get; set; }

    public virtual ICollection<CaseReply> Replies { get; set; }
}

// CaseReply.cs (CaseReply Object) // CaseReply.cs(CaseReply对象)

public class CaseReply
{
    [Key]
    public int Id { get; set; }

    public string Message { get; set; }

    public DateTime Date { get; set; }

    public int CaseId { get; set; }

    public Guid UserId { get; set; }

    public virtual User User { get; set; }

    public virtual Case Case { get; set; }
}

// RepositoryBase.cs // RepositoryBase.cs

public class RepositoryBase<T> : IRepository<T> where T : class
{
    public IDbContext Context { get; private set; }
    public IDbSet<T> ObjectSet { get; private set; }

    public RepositoryBase(IDbContext context)
    {
        Contract.Requires(context != null);

        Context = context;

        if (context != null)
        {
            ObjectSet = Context.CreateDbSet<T>();

            if (ObjectSet == null)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public IRepository<T> Remove(T entity)
    {
        ObjectSet.Remove(entity);
        return this;
    }

    public IRepository<T> SaveChanges()
    {
        Context.SaveChanges();
        return this;
    }
}

// CaseRepository.cs // CaseRepository.cs

public class CaseRepository : RepositoryBase<Case>, ICaseRepository
{
    public CaseRepository(IDbContext context)
            : base(context)
    {
        Contract.Requires(context != null);
    }

    public bool RemoveCaseReplyFromCase(int caseId, int caseReplyId)
    {
        Case caseToRemoveReplyFrom = ObjectSet.Include(x => x.Replies).FirstOrDefault(x => x.Id == caseId);
        var delete = caseToRemoveReplyFrom.Replies.FirstOrDefault(x => x.Id == caseReplyId);

        caseToRemoveReplyFrom.Replies.Remove(delete);

        return Context.SaveChanges() >= 1;
    }
}

Thanks in advance. 提前致谢。

If you want EF to remove CaseReply from DB when you remove it from parent's object collection, you have to use Identifying relations. 如果希望EF在从父对象的对象集合中删除CaseReply从数据库中删除CaseReply ,则必须使用标识关系。 It means that your CaseReply must have composite primary key based on Id and CaseId columns. 这意味着您的CaseReply必须具有基于IdCaseId列的复合主键。

I believe you are using EF CT4? 我相信您正在使用EF CT4? I had the same problem too and hated it. 我也有同样的问题,讨厌它。 I think your code is totally fine. 我认为您的代码完全可以。

Since your association is one to many, the only solution is to setting cascading delete at database side. 由于您的关联是一对多的,因此唯一的解决方案是在数据库端设置级联删除。 < http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx > < http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx >

The above solution works for deleting orphans on deleting their parent record. 上述解决方案适用于在删除父记录时删除孤儿的情况。 I hope it will solve your issue too. 我希望它也能解决您的问题。

You have to remove from context.Replies if you want to delete it in db. 您必须从上下文中删除。如果要在数据库中删除它,请回复。 What you are doing right now is removing it from case's replies collection which removes the association. 您现在正在做的是从case的replies集合中删除它,从而删除了关联。 So you have to call: 因此,您必须致电:

context.Replies.Remove(delete)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF 5代码首先,具有级联删除功能的多个父级子级? - EF 5 Code First, multiple parent child with cascade delete? LINQ /代码优先:如何从父对象中删除子对象的引用? - LINQ/Code first: how do I delete a child object's reference from the parent object? EF4 代码优先 - 从数据库中删除父级时删除所有子级? - EF4 Code first - delete all children when deleting parent from db? 为什么先更新EF代码中的父实体和子实体时,为什么不必更新父对象 - Why don't I have to update a parent object when updating both the parent and child entity in EF code first 多个父实体首先在EF代码中包含一个子实体 - multiple parent Entities with one child entity in EF code first EF 4.1父级子代和继承映射的代码优先问题 - EF 4.1 Code First problem with parent child and Inheritance Mapping 首先使用EF代码,子级包含父级对象列表 - EF code first, child holds a list of parent objects 如何从 EF Core 中的父实体中删除子项? - How do you delete Child from Parent entities in EF Core? 在EF中,通过删除子行克隆中首先引用它的子行来删除父行是否有效? - In EF, is it efficient to delete a parent row by deleting the child rows referencing it first through the child row clones? 从 EF Core 中的父对象访问子对象 - Accessing child object from parent object in EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM