LINQ to Entities how to update a record

后端 未结 4 1046
粉色の甜心
粉色の甜心 2021-02-01 01:58

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:

4条回答
  •  情歌与酒
    2021-02-01 02:45

    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}

提交回复
热议问题