14

I have some data coming from other tiers and it represents an EF object. When it's new, I do this:

context.AddToCustomer(mynewobject);
context.SaveChanges();

but now my data forms an existing object, so I want the context to know I want to update the data and not inserting it.

I've seen 'ApplyPropertyChanges' but I can't figure out how to use it. I've also seen people doing this:

Customer existingOne = (from n in context.Customers 
                        where n.id = mynewobject.id select n).First()
existingOne.name = mynewobject.name
existingOne.address= mynewobject.address
context.SaveChanges();

but that seems a little odd because I have to manually set all the props AND read the entire object first.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Michel
  • 23,085
  • 46
  • 152
  • 242

4 Answers4

8

In Entity Framework 5 this is how you go about:

        /// <summary>
        /// Updates an entity
        /// </summary>
        /// <param name="input">A entity</param>
        /// <returns>The updated object</returns>
        public TEntity Update(TEntity input)
        {
            using (var context = GetContext())
            {
                context.Set<TEntity>().Attach(input);

                var entry = context.ChangeTracker.Entries<TEntity>().FirstOrDefault(e => e.Entity == input);
                if (entry != null)
                    entry.State = EntityState.Modified;

                context.SaveChanges();
            }

            return input;
        }
Andreas
  • 5,501
  • 2
  • 20
  • 23
4

While I question whether it's even worthwhile to "optimize" an update, you can nevertheless do what you ask. It's easier in EF 4, but also possible in EF 1. See also this article.

public static void AttachAsModified<T>(this ObjectSet<T> objectSet, T entity) where T : class
{
    objectSet.Attach(entity);
    objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    Just to add that things get a bit more complicated with EF1 when you want to update other properties then scalar (as you are going to be able to see at "Caveats" section of "possible in EF1" article). You can find workaround for that between answers to this question http://stackoverflow.com/questions/1612655/entity-framework-updating-with-related-entity – Misha N. Nov 11 '09 at 14:08
  • Thanks very much for the answer. Sometimes you just have to see it to believe it. I couldn't have figured this out myself, the 'possible in EF 1' solution. – Michel Nov 11 '09 at 14:10
0

There is on of my post to execute this

Community
  • 1
  • 1
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
0

Taken from Employee Info Starter Kit, you can consider the code snippet as below:

public void UpdateEmployee(Employee updatedEmployee)
        {
            //attaching and making ready for parsistance
            if (updatedEmployee.EntityState == EntityState.Detached)
                _DatabaseContext.Employees.Attach(updatedEmployee);
            _DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
            _DatabaseContext.SaveChanges();
        }
Ashraf Alam
  • 3,500
  • 32
  • 31