LINQ到实体如何更新记录 [英] LINQ to Entities how to update a record
问题描述
好了,所以我是新来的都EF和LINQ。我已经想通了如何插入和删除,但由于某些原因的UPDATE似乎逃避我的掌握
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.
下面是我的代码示例:
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.
现在我该如何更新?我似乎无法找到一个 UpdateObject()
对实体采集方法。
Now how do I update? I can't seem to find an "UpdateObject()
" method on the entity collection.
推荐答案
只需修改一个返回的实体:
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();
请注意,您只能更新一个实体(东西延伸EntityObject,不是你已经使用类似选择新CustomObject {名= x.Name}
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}
这篇关于LINQ到实体如何更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!