How do I update an EF object when passing a modified object as a parameter?

I have a quick question.

Assuming the context is my EF context and the Responder is the actual EF object created by EF from the database.

What is the shortest way to update the Respondent ?

public void UpdateRespondent(Respondent respondent)
{
    var resp = context.Respondents.First(r => r.RespondentId == respondent.RespondentId);

    // Now... do I have to copy all properties from the respondent into resp ??
    // But respondent is actually the Respondent entity
    // Can I just replace it somehow?

    context.SaveChanges();
}

      

Thanks a bunch.

Update1

Thanks to nrodic, this code works like a charm:

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

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

      

One question: it looks like I don't need the first line "var respent =" at all !!

Any idea why this line is present in many examples?

Thank.

Update2

Hmm, it looks like I need this first line. Otherwise it throws an exception on the second line (db.Respondents.ApplyCurrentValues โ€‹โ€‹(changed);)

An object with a key that matches the key of the supplied object may not be found in the ObjectStateManager. Make sure the key values โ€‹โ€‹of the supplied object match the key values โ€‹โ€‹of the object to which the changes are to be applied.

enter image description here

+3


source to share


2 answers


You can do it:

    public void UpdateRespondent(Respondent respondent)
{
    var resp = context.Respondents.First(r => r.RespondentId == respondentId);

    // Now... do I have to copy all properties from the respondent into resp ??
    // But respondent is actually the Respondent entity
    // Can I just replace it somehow?

    resp.Name = "Bob";
    resp.SomeProperty = "SomeValue";
    context.SaveChanges();
}

      



You can simply update the properties of the resp object. Although you see the first comment, it may not be exactly what you want.

0


source


If you load an object, detach it from the context, and update its properties, you can apply the changes to the database using the ApplyCurrentValues()

. Use it like this:

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

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

      

Note that when called, the ApplyCurrentValues()

object must be bound to a context (read from the database). Otherwise, it InvalidOperationException

will select:

An object with a key that matches the key of the supplied object may not be found in the ObjectStateManager. Make sure the key values โ€‹โ€‹of the supplied object match the key values โ€‹โ€‹of the object to which the changes are to be applied.



If you use DbContext

instead ObjectContext

, be sure to read this question .


Another way to update the database could be using an auto-matching technique ( Automapper or ValueInjecter , for example). This is a more general case because it allows DTOs to be used and provides good control over what (and how) should be updated.

0


source







All Articles