实体框架问题 - 我正在寻找更有效的更新父母和子女记录的方法。
场景 - 我们有一个包含一个或多个子对象的父对象。当父对象更新时,我们需要清除所有当前子记录并重新添加新的子记录。
例如:
public bool UpdateProfile(Parent record)
{
try
{
_context.Parent.Attach(record);
_context.Entry(record).State = EntityState.Modified;
List<Child> childrenToDelete = GetChildrenByParentId(parent.Id);
foreach (Child child in childrenToDelete)
{
_context.Child.Remove(child);
_context.Entry(child).State = EntityState.Deleted;
}
foreach (Child child in record.children)
{
_context.ProfileCategories.Add(child);
}
_context.SaveChanges();
return true;
}
...
}
上面的代码在“_context.Parent.Attach(record)”行引发异常,说明有重复的记录ID。所以我们的工作是:
public bool UpdateProfile(Parent record)
{
try
{
var originalChildren = record.children;
record.children = new List<Child>();
_context.Parent.Attach(record);
_context.Entry(record).State = EntityState.Modified;
_context.SaveChanges();
List<Child> childrenToDelete = GetChildrenByParentId(parent.Id);
foreach (Child child in childrenToDelete)
{
_context.Child.Remove(child);
_context.Entry(child).State = EntityState.Deleted;
}
foreach (Child child in originalChildren)
{
_context.ProfileCategories.Add(child);
}
_context.SaveChanges();
return true;
}
...
}
这第二段代码有效,但我觉得它不理想。
有人可以告诉我们为什么Attach会抛出重复的Id异常,如果我们的解决方案是解决这个问题的最佳方法吗?
答案 0 :(得分:0)
未经测试,但我会拍摄类似的东西:
public void UpdateProfile(Parent record)
{
using(var db = new MyContext())
{
var children = db.Children.Where(x => x.ParentId == record.Id);
db.ProfileCategories.AddRange(children);
db.Children.RemoveRange(children);
db.SaveChanges();
}
}