开发者

Is there an Update Object holder on Entity Framework?

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.


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)


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();
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜