Is there an Update Object holder on Entity Framework?

对着背影说爱祢 提交于 2019-12-08 06:14:51

问题


I'm currently inserting/updating fields like this (if there's a better way, please say so - we're always learning)

public void UpdateChallengeAnswers(List<ChallengeAnswerInfo> model, Decimal field_id, Decimal loggedUserId)
{
    JK_ChallengeAnswers o;
    foreach (ChallengeAnswerInfo a in model)
    {
        o = this.FindChallengeAnswerById(a.ChallengeAnswerId);
        if (o == null) o = new JK_ChallengeAnswers();

        o.answer = FilterString(a.Answer);
        o.correct = a.Correct;
        o.link_text = "";
        o.link_url = "";
        o.position = FilterInt(a.Position);

        o.updated_user = loggedUserId;
        o.updated_date = DateTime.UtcNow;

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;  // FK
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            this.Save();
        }
    }

    this.Save(); // Commit changes
}

As you can see there is 2 times this.Save() (witch invokes db.SaveChanges();)

when Adding we place the new object into a Place Holder with the AddObject method, in other words, the new object is not committed right away and we can place as many objects we want.

But when it's an update, I need to Save first before moving on to the next object, is there a method that I can use in order to, let's say:

        if (o.challenge_id == 0)
        {
            // New record
            o.challenge_id = field_id;
            o.created_user = loggedUserId;
            o.created_date = DateTime.UtcNow;

            db.JK_ChallengeAnswers.AddObject(o);
        }
        else
        {
            // Update record
            db.JK_ChallengeAnswers.RetainObject(o);
        }
    }

    this.Save(); // Only save once when all objects are ready to commit
}

So if there are 5 updates, I don't need to save into the database 5 times, but only once at the end.

Thank you.


回答1:


Well if you have an object which is attached to the graph, if you modify values of this object, then the entity is marked as Modified.

If you simply do .AddObject, then the entity is marked as Added.

Nothing has happened yet - only staging of the graph.

Then, when you execute SaveChanges(), EF will translate the entries in the OSM to relevant store queries.

Your code looks a bit strange. Have you debugged through (and ran a SQL trace) to see what is actually getting executed? Because i can't see why you need that first .Save, because inline with my above points, since your modifying the entities in the first few lines of the method, an UPDATE statement will most likely always get executed, regardless of the ID.

I suggest you refactor your code to handle new/modified in seperate method. (ideally via a Repository)




回答2:


Taken from Employee Info Starter Kit, you can consider the code snippet as below:

public void UpdateEmployee(Employee updatedEmployee)
        {
            //attaching and making ready for parsistance
            if (updatedEmployee.EntityState == EntityState.Detached)
                _DatabaseContext.Employees.Attach(updatedEmployee);
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
            _DatabaseContext.SaveChanges();
        }


来源:https://stackoverflow.com/questions/4131868/is-there-an-update-object-holder-on-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!