Slava
Slava

Reputation: 6640

How to update the whole list of entities with ApplyCurrentValues?

Ok,

here is how I update a single object:

public void UpdateRespondent(Respondent changed)
{
    var respondent = db.Respondents.FirstOrDefault(r => r.RespondentId == changed.RespondentId);

    db.Respondents.ApplyCurrentValues(changed);
    db.SaveChanges();
}

This will invoke 1 select and 1 update (if something changed).

Now, if I have a List<Respondent> with hundreds of them, how do I do that, call UpdateRespondent(changed) on each one in a loop? That will result in hundreds * 2 sql statements.

Or is there a more efficient way to do this?

Thanks.

Upvotes: 0

Views: 184

Answers (1)

sgeddes
sgeddes

Reputation: 62831

EF does not support batch updates. You can look into using EntityFramework Extended Library -- I've seen others who've had success using it:

https://github.com/loresoft/EntityFramework.Extended

Code sniplet from above link:

//update all tasks with status of 1 to status of 2
context.Tasks.Update(
    t => t.StatusId == 1, 
    t2 => new Task {StatusId = 2});

//example of using an IQueryable as the filter for the update
var users = context.Users.Where(u => u.FirstName == "firstname");
context.Users.Update(users, u => new User {FirstName = "newfirstname"});

Alternatively, you could use straight ADO.Net and call a stored proceudre.

Good luck.

Upvotes: 1

Related Questions