[英]GraphDiff causing id of entity to not get populated after saveChanges()
When using graphDiff for creating a record the new id isn't getting populated back into the entity after saveChanges() is called. 当使用graphDiff创建记录时,在调用saveChanges()之后,新的id不会被填充回实体。 Its been logged on the github repo here - https://github.com/refactorthis/GraphDiff/issues/144 but the repo doesn't seem to be that active any more so was hoping someone on here would have some insight on how to get this working.
它已经登录了github repo这里 - https://github.com/refactorthis/GraphDiff/issues/144但是回购似乎没那么活跃了所以希望有人在这里会有一些见解如何让这个工作。
How it should work however qualification.Id is always 0 for a create. 它应该如何工作但是qual.Id对于创建总是0。 For an update it's the correct Id.
对于更新,这是正确的ID。
public int CreateUpdate(Qualification qualification)
{
using (var db = new DataContext())
{
db.UpdateGraph(qualification);
db.SaveChanges();
return qualification.Id;
}
}
My current work around is to not use graph diff for creates but this isn't ideal. 我目前的工作是不使用graph diff进行创建,但这并不理想。
public int CreateUpdate(Qualification qualification)
{
using (var db = new DataContext())
{
if (qualification.Id == 0)
{
db.Entry(qualification).State = EntityState.Added;
}
else
{
db.UpdateGraph(qualification);
}
db.SaveChanges();
return qualification.Id;
}
}
Thanks 谢谢
So I found the solution - https://github.com/refactorthis/GraphDiff/issues/32#issuecomment-34420859 所以我找到了解决方案 - https://github.com/refactorthis/GraphDiff/issues/32#issuecomment-34420859
Importantly "GraphDiff hands you a new instance back" so you need to set the return to your entity. 重要的是“GraphDiff将您的新实例发回给您”,因此您需要设置实体的回报。
public int CreateUpdate(Qualification qualification)
{
using (var db = new DataContext())
{
qualification = db.UpdateGraph(qualification);
db.SaveChanges(qualification.AuditUserId);
return qualification.Id;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.