0
我試圖簡單地更新實體對象,我得到這個錯誤..所有使用googling我犯的錯誤帶我到複雜解釋......任何人都可以簡單地說出來嗎?。在ObjectStateManagerObjectStateManager中已存在的對象具有相同的鍵已經存在不能使用相同的密鑰追蹤多個對象
我工作的這個簡單的教程
else
{
//UPDATE
int iFid = Int32.Parse(fid.First().fid.ToString());
oFinancial.fid = iFid;
oFinancial.mainqtr = currentQuarter;
oFinancial.mainyear = currentYear;
oFinancial.qtr = Int32.Parse(currentQuarter);
oFinancial.year = Int32.Parse(currentYear);
oFinancial.updatedate = DateTime.Now;
// ObjectStateEntry ose = null;
// if (!dc.ObjectStateManager.TryGetObjectStateEntry(oFinancial.EntityKey, out ose))
// {
dc.financials.Attach(oFinancial);
// }
dc.ObjectStateManager.ChangeObjectState(oFinancial, System.Data.EntityState.Modified);
}
dc.SaveChanges();
這裏是什麼,是在我使用簡單的代碼越往上讓我的主鍵值..可能是一個更好的方式但它的工作原理。
var fid = from x in dc.financials
where iPhaseID == x.phaseid &&
strTaskID == x.ftaskid &&
strFundType == x.fundtype &&
iCurrentQuarter == x.qtr &&
iCurrentYear == x.year
select x;
是fid是主鍵..但我需要獲得EF的行的主鍵,以便它更新它我會猜測..生病明天看看它 – punkouter 2010-09-23 21:13:13
yup。這似乎解決了它..我正在把東西我甚至不明白......也許有一個更優雅的方式來獲得主鍵雖然? – punkouter 2010-09-23 21:21:49
如果您有財務記錄,您應該已經知道它是什麼。你可以查詢該對象的直流,然後更新任何屬性,然後調用SaveChanges(),並且一切都自動發生:) – 2010-09-23 21:31:23