3

DataContext.ApplyCurrentValues() needs entitySetName, what is it?

I think that code would same:

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

Is it correct?

FreeVice
  • 2,567
  • 6
  • 21
  • 28

3 Answers3

3

This is for .Net 4.0

For this example, lets assume 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();
}

If you are using .Net 4.1 then you can use "context.Entry(...)" instead of "context.ObjectStateManager.ChangeObjectState(product, EntityState.Modified)" as seen here: Example of context.Entry()

This is the most straight forward way to do it. It doesn't require that you pull the object from the DB first, you can just provide an object you were tinkering with. The only downside is that this updates all fields, not just a single field.

Community
  • 1
  • 1
dyslexicanaboko
  • 4,215
  • 2
  • 37
  • 43
0

see this article http://msdn.microsoft.com/en-us/library/bb738695.aspx and http://msdn.microsoft.com/en-us/library/bb386870.aspx sample about update with EF.
And this SO question about ApplyCurrentValues: ApplyCurrentValues in EF 4 to see how to work with AppliCurrentValues.

Community
  • 1
  • 1
0x49D1
  • 8,505
  • 11
  • 76
  • 127
0

Are you using EF4 - do this instead then you don't need to worry about the Entity Set Name:

DataContext.Product.ApplyCurrentValues(p);
Rob
  • 10,004
  • 5
  • 61
  • 91