我的代碼是這樣的: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? }}現(xiàn)在請告訴我我應(yīng)該在Update()方法中寫些什么?我寫的所有內(nèi)容都會(huì)遇到各種異常。(請注意,加載的數(shù)據(jù)已被跟蹤,連接或類似的操作)
3 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
摘自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 關(guān)注
- 473 瀏覽
添加回答
舉報(bào)
0/150
提交
取消