[英]updating related data in entity framework
I have an update function in my repository which updates TerminalCertification entity.我的存储库中有一个更新 function 更新了 TerminalCertification 实体。 But this entity has a many to many relation to another class ( GomrokJustification ).
但是这个实体与另一个 class ( GomrokJustification ) 有多对多的关系。
my update function update entity correctly but does not anything on related entity.我的更新 function 正确更新实体,但对相关实体没有任何作用。 my update function is below:
我的更新 function 如下:
public void UpdateTerminalCertification(TerminalCertification terminalCertification)
{
var lastCertification =
db.terminalCertifications.Include("TimeInfo").Include("GomrokJustifications").Where(item=>item.TerminalCertificationID==terminalCertification.TerminalCertificationID).ToList();
if (lastCertification.Count==0)
throw new TerminalCertificationNotFoundException(terminalCertification);
terminalCertification.TimeInfo = lastCertification[0].TimeInfo;
((IObjectContextAdapter)db).ObjectContext.Detach(lastCertification[0]);
((IObjectContextAdapter)db).ObjectContext.AttachTo("terminalCertifications", terminalCertification);
foreach (var gomrokJustification in terminalCertification.GomrokJustifications)
{
((IObjectContextAdapter)db).ObjectContext.AttachTo("gomrokJustifications", gomrokJustification);
((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.ChangeObjectState(gomrokJustification, EntityState.Modified);
}
((IObjectContextAdapter) db).ObjectContext.ObjectStateManager.ChangeObjectState(terminalCertification,EntityState.Modified);
}
and my TerminalCetrification has a list of GomrokJustifications which was filled before by some entities.我的 TerminalCetrification 有一个 GomrokJustifications 列表,之前由一些实体填写。 I want to those last entity being replaced by new ones.
我希望最后一个实体被新实体取代。 but this was not happen.
但这并没有发生。 does anyone have any idea?
有人有什么主意吗?
Instead of doing this:而不是这样做:
var lastCertification = db.terminalCertifications
.Include("TimeInfo")
.Include("GomrokJustifications")
.Where(item=>item.TerminalCertificationID==terminalCertification.TerminalCertificationID)
.ToList();
if (lastCertification.Count==0)
throw new TerminalCertificationNotFoundException(terminalCertification);
you could just do this:你可以这样做:
var lastCertification = db.terminalCertifications
.Include("TimeInfo")
.Include("GomrokJustifications")
.Where(item=>item.TerminalCertificationID==terminalCertification.TerminalCertificationID)
.FirstOrDefault();
if (lastCertification == null)
throw new TerminalCertificationNotFoundException(terminalCertification);
First throws an exception if there are no elements in the collection, so if you don't care about the terminalcertificationnotfoundexception you could even remove that custom exception.如果集合中没有元素,First 会引发异常,因此如果您不关心 terminalcertificationnotfoundexception,您甚至可以删除该自定义异常。 Your logic even seems to assume that there will be only one element in the returned list so you could even use Single().
您的逻辑甚至似乎假设返回列表中只有一个元素,因此您甚至可以使用 Single()。 That expresses more what you want to achieve compared to calling tolist and then retrieving the first item.
与调用 tolist 然后检索第一项相比,这更能表达您想要实现的目标。
We'll need some more info to help you along.我们需要更多信息来帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.