简体   繁体   中英

updating related data in entity framework

I have an update function in my repository which updates TerminalCertification entity. But this entity has a many to many relation to another class ( GomrokJustification ).
my update function update entity correctly but does not anything on related entity. my update function is below:

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. 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. Your logic even seems to assume that there will be only one element in the returned list so you could even use Single(). That expresses more what you want to achieve compared to calling tolist and then retrieving the first item.

  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. 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

We'll need some more info to help you along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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