简体   繁体   English

更新实体框架中的相关数据

[英]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 然后检索第一项相比,这更能表达您想要实现的目标。

  1. After looking carefully at your code I actually don't get the point you are trying to achieve here.仔细查看您的代码后,我实际上不明白您在这里想要达到的目的。 You have an existing terminalcertification entity to start with, you then retrieve it again in that first query, why?您有一个现有的终端认证实体开始,然后在第一个查询中再次检索它,为什么? You then take the timeinfo from conceptually the same entity (cause you did a get by id) to the one you get as input parameter.然后,您将 timeinfo 从概念上相同的实体(因为您通过 id 获取)获取到您作为输入参数获得的实体。 Why not continue working on the one that was retrieved from the database?为什么不继续处理从数据库中检索到的那个呢? You then detach the entity you received from the database, why?然后你从数据库中分离你收到的实体,为什么? And continue working with the input terminalcertification.并继续使用输入终端认证。 I think you need to look a bit more carefully on the entity framework documentation about entity state etc. Take a look at ApplyCurrentValues and detaching and attaching objects here: http://msdn.microsoft.com/en-us/library/bb896271.aspx我认为您需要更仔细地查看有关实体 state 等的实体框架文档。在此处查看 ApplyCurrentValues 以及分离和附加对象: http://msdn.microsoft.com/en-us/library/bb896271。 aspx

We'll need some more info to help you along.我们需要更多信息来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM