41

Okay, so I'm new to both EF and LINQ. I have figured out how to INSERT and DELETE but for some reason UPDATE seems to escape my grasp.

Here is a sample of my code:

EntityDB dataBase = new EntityDB();
Customer c = new Customer
{
     Name = "Test",
     Gender = "Male
};
dataBase.Customers.AddObject(c);
dataBase.SaveChanges();

The above creates and adds a record just fine.

Customer c = (from x in dataBase.Customers
             where x.Name == "Test"
             selext x).First();
dataBase.Customers.DeleteObject(c);
dataBase.SaveChanges();

The above effectively deletes the specified record.

Now how do I update? I can't seem to find an "UpdateObject()" method on the entity collection.

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • If you are using EF4, I'd prefer this solution http://stackoverflow.com/questions/623672/update-entity-framework-objects – D.Rosado Dec 07 '12 at 10:38

4 Answers4

84

Just modify one of the returned entities:

Customer c = (from x in dataBase.Customers
             where x.Name == "Test"
             select x).First();
c.Name = "New Name";
dataBase.SaveChanges();

Note, you can only update an entity (something that extends EntityObject, not something that you have projected using something like select new CustomObject{Name = x.Name}

erictrigo
  • 989
  • 2
  • 13
  • 41
tster
  • 17,883
  • 5
  • 53
  • 72
  • I did that. For some reason it is not persisting the changes to the database. – CatDadCode Jan 06 '11 at 17:37
  • @Chevex: But note that it get a lot more involved when using another context instance. – H H Jan 06 '11 at 17:47
  • What does it mean to "use another context instance"? – CatDadCode Jan 06 '11 at 17:52
  • I've tried it, but for some reason, in my case it duplicates instead of update. The old record is maintained and a new record is created with the information of the record I suppose to update. What is happening? – cgalvao1993 Dec 03 '13 at 17:39
8

//for update

(from x in dataBase.Customers
         where x.Name == "Test"
         select x).ToList().ForEach(xx => xx.Name="New Name");

//for delete

dataBase.Customers.RemoveAll(x=>x.Name=="Name");
Mamun
  • 66,969
  • 9
  • 47
  • 59
Zohaib Iqbal
  • 263
  • 1
  • 4
  • 6
3

They both track your changes to the collection, just call the SaveChanges() method that should update the DB.

Anthony
  • 1,651
  • 1
  • 17
  • 31
0

In most cases @tster's answer will suffice. However, I had a scenario where I wanted to update a row without first retrieving it.

My situation is this: I've got a table where I want to "lock" a row so that only a single user at a time will be able to edit it in my app. I'm achieving this by saying

update items set status = 'in use', lastuser = @lastuser, lastupdate = @updatetime where ID = @rowtolock and @status = 'free'

The reason being, if I were to simply retrieve the row by ID, change the properties and then save, I could end up with two people accessing the same row simultaneously. This way, I simply send and update claiming this row as mine, then I try to retrieve the row which has the same properties I just updated with. If that row exists, great. If, for some reason it doesn't (someone else's "lock" command got there first), I simply return FALSE from my method.

I do this by using context.Database.ExecuteSqlCommand which accepts a string command and an array of parameters.

Just wanted to add this answer to point out that there will be scenarios in which retrieving a row, updating it, and saving it back to the DB won't suffice and that there are ways of running a straight update statement when necessary.

Troy Frazier
  • 391
  • 3
  • 13