[英]ASP MVC 5 - Remove Child Entities From Parent
How can I remove All child entities from a one to many relationship via the parent - Note I only want to remove the children. 如何通过父级从一对多关系中删除所有子级实体-注意,我只想删除子级。
public class Parent
{
public int Id { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public virtual Parent Parent { get; set; }
}
Is there a shorthand way, lets say something like below: 有没有一种简便的方法,让我们说以下话:
parent.children.Remove();
I believe that your mechanism for deleting the child elements in EF 4 and above should be: 我相信您在EF 4及更高版本中删除子元素的机制应为:
parent.children.ToList().ForEach(c => context.Children.Remove(c));
context.SaveChanges();
I have found in the past that it may be faster to write a DELETE
yourself, but you should decide what is best for you. 过去,我发现自己编写
DELETE
可能会更快,但是您应该确定最适合自己的方法。 Introducing a SQL DELETE
in an Entity Framework solution adds some complexity both to testing and because your DELETE
will depend on things you may have been trying to avoid as EF takes care of it all. 在Entity Framework解决方案中引入SQL
DELETE
既增加了测试的复杂性,又因为DELETE
将取决于您可能试图避免的事情,因为EF会全力以赴。 Always measure before you optimize and decide what trade-offs work for you. 在进行优化之前,请务必进行衡量并确定哪些折衷方案对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.