Saving a modified object from ASP.NET MVC View Using Entity-Franework 4

Posted by Dani on Stack Overflow See other posts from Stack Overflow or by Dani
Published on 2010-12-29T17:49:18Z Indexed on 2010/12/29 17:54 UTC
Read the original article Hit count: 160

I retrieve an object graph from DB using EF4. The context is closed as soon as the data retrieve and the data passes to the controller, and then to the view. in the view the data is modified, and then the controller gets it back.

From the controller I run Repository.Update(MyEmp);

and in my repository the code goes:

using (var context = new mydb())

        {
            if (myEmp.ID != 0)  // Checking if it's modified or new
            {

                context.Emp.Attach(MyEmp);
                int result = context.SaveChanges();
                return myEmp.ID;
            }
        }

The problem - once attached, the object entityState goes to unchanged, and not modified, and of course - nothing is saved to the database.

What am I doing wrong ?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about entity-framework-4