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