這是更新代碼,我發現:LINQ到實體更新記錄到數據庫
using (TestDBEntities ctx = new TestDBEntities())
{
//Get the specific employee from Database
Emp e = (from e1 in ctx.Emp
where e1.Name == "Test Employee"
select e1).First();
//Change the Employee Name in memory
e.Name = "Changed Name";
//Save to database
ctx.SaveChanges();
}
現在我在做什麼是這樣的:
using(CRNNSTestEntities crnnsupContext = new CRNNSTestEntities())
{
CPersonalInfo t = ((IQueryable<CPersonalInfo>)Cache["personquery"]).First();
t.Tombstone.Address = Address1.Text;
System.Windows.Forms.MessageBox.Show(crnnsupContext.SaveChanges()+"");
};
不工作。所以我的問題是我必須寫一些像CPersonalInfo t = from t in ....
爲什麼我的方法不工作?
感謝
這是否意味着每次我想更新記錄時,我都必須先運行搜索查詢? – pita 2012-04-04 13:12:22
@pita - 是的,您需要從您正在調用的上下文* SaveChanges()*中檢索記錄。 – Aducci 2012-04-04 13:15:30