我的代码是这样的:public class Program{ [STAThread] static void main() { DataAccessClass dal = new DataAccessClass(); List<Person> list = dal.GetPersons(); Person p = list[0]; p.LastName = "Changed!"; dal.Update(p); }}public class DataAccessClass{ public static List<Person> GetPersons() { MyDBEntities context = new MyDBEntities(); return context.Persons.ToList(); } public void Update(Person p) { // what sould be written here? }}现在请告诉我我应该在Update()方法中写些什么?我写的所有内容都会遇到各种异常。(请注意,加载的数据已被跟踪,连接或类似的操作)
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
摘自Employee Info Starter Kit,您可以考虑以下代码片段:
public void UpdateEmployee(Employee updatedEmployee)
{
//attaching and making ready for parsistance
if (updatedEmployee.EntityState == EntityState.Detached)
_DatabaseContext.Employees.Attach(updatedEmployee);
_DatabaseContext.ObjectStateManager.ChangeObjectState(updatedEmployee, System.Data.EntityState.Modified);
_DatabaseContext.SaveChanges();
}
- 3 回答
- 0 关注
- 451 浏览
添加回答
举报
0/150
提交
取消