In Entity Framework, how do you change an object?

ado.net c# entity-framework

Question

What is the entitySetName that DataContext.ApplyCurrentValues() requires?

I believe that code would be the same:

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

Is it accurate?

1
3
2/16/2011 9:13:21 AM

Accepted Answer

see the example http://msdn.microsoft.com/en-us/library/bb738695.aspx and http://msdn.microsoft.com/en-us/library/bb386870.aspx regarding update using EF in this post.
Additionally, please refer to this SO question regarding ApplyCurrentValues: EF 4's ApplyCurrentValues to learn how to use it.

0
5/23/2017 12:06:32 PM

Popular Answer

This applies to.Net 4.0.

Let's suppose we are working with Product objects for the sake of this example.

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

You may use.Net 4.1 if you have it installed "Instead of "context.ObjectStateManager," use "context.Entry(...)". (Product, EntityState.Modified) ChangeObjectState "like this: instance of a situation Entry()

The easiest approach to accomplish it is in this fashion. You may merely provide an item you were fiddling with; it's not necessary to get the object from the database first. The only drawback is that all fields, not just one, are updated.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow