[英]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
必须具有基于Id
和CaseId
列的复合主键。
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.