How to edit an object in the Entity Framework?

DataContext.ApplyCurrentValues ​​() requires entitySetName, what is it?

I think the code will be the same:

    public void Edit(Products p)
    {
        DataContext.ApplyCurrentValues("Products", p);
        DataContext.SaveChanges();
    }

Is it correct?

+3
source share
3 answers

see this article http://msdn.microsoft.com/en-us/library/bb738695.aspx and http://msdn.microsoft.com/en-us/library/bb386870.aspx an example on updating using EF.
And this SO question is about ApplyCurrentValues: ApplyCurrentValues ​​in EF 4 to see how to work with AppliCurrentValues.

0
source

This is for .Net 4.0

In this example, suppose we are dealing with Product objects.

using (DBEntities context = new DBEntities())
{
    //Must attach first and change the state to modified
    context.Products.Attach(product);

    //If you are using .Net 4.1 then you can use this line instead:
    //context.Entry(
    context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);

    context.SaveChanges();
}

.Net 4.1, "context.Entry(...)" "context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified)", : context.Entry()

. , , , . , , .

+3

Do you use EF4 - do it instead, you don’t have to worry about the name of the set of objects:

DataContext.Product.ApplyCurrentValues(p);
0
source

Source: https://habr.com/ru/post/1792460/


All Articles