开发者

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:

publ开发者_开发问答ic 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜