entity framework 如何批量更新子表

kamYee 2018-04-20 11:21:00
我现在有三个model,关系如下

public class Test
{
[Key]
public string ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Test1> Test1List { get; set; }
public virtual ICollection<Test2> Test2List { get; set; }
}
public class Test1
{
[Key]
public string ID { get; set; }
public string Title { get; set; }
public virtual Test Parent { get; set; }
}
public class Test2
{
[Key]
public string ID { get; set; }
public string Title { get; set; }
public virtual Test Parent { get; set; }
}

我在更新的时候传入了Test对象,如果单独只更新Test是正常的,但是如果更新包含的关联表就会出现Error
               public bool Eidt(Testl model)
{
try
{
using (var db = new db_contex())
{
db.Configuration.LazyLoadingEnabled = false;
Testl tm = db.Test.Find(model.ID);
tm.Title=model.Title;
tm.Test1List=model.Test1List;
tm.Test2List=model.Test2List;
return db.SaveChanges() > 0;
}
}
catch (Exception ex)
{
CommonHelper.WriteLog("Eidt", ex.Message);
return false;
}
}

错误信息如下:
Multiplicity constraint violated. The role 'Test1_Parent_Target' of the relationship 'Models.Test1_Parent' has multiplicity 1 or 0..1.
我针对这个错误查询了相关资料,给出的解决方案都是一条条查出来单独更新,想请问一下有没有批量更新的方案;或者这个错误该如何解决
...全文
334 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧