How can I use Entity Framework code to update a row first?

code-first entity-framework

Question

How do I go about changing a database row? There is no means for updating the data, and if I use add but the main key id is already there, I receive an error. If you can, give us an example.

1
9
8/23/2014 9:11:30 PM

Accepted Answer

The simplest method is:

(1) Using pk, obtain the current row.

(2) Refresh properties

(3) use context's SaveChanges() method.

e.g.

        var student = context.Students.Find(42);

        student.Description = "updated";

        context.SaveChanges();
21
2/8/2011 4:11:29 PM

Popular Answer

Here is a technique I used that didn't need me to first ask a question:

context.Students.Attach(student);
context.Entry(student).State = EntityState.Modified;
context.SaveChanges();


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