【发布时间】:2011-12-11 23:15:33
【问题描述】:
你们中的一个可爱的人能否解释一下为什么这段代码不起作用或执行它的更好/正确的方式......
CandidDomainService db = new CandidDomainService();
GlobalValuesTable newInstanceId =
db.GetGlobalValuesTables().Single<GlobalValuesTable>(gId => gId.RowId == 1);
newInstanceId.CurrentTestInstance++;
db.UpdateGlobalValuesTable(newInstanceId);
它在db.Update 处引发null 异常。我正在使用生成的添加、更新、删除和获取。该异常被描述为在没有对实体模型进行任何更改时引发。
谢谢!
编辑:
这是生成的更新代码……
public void UpdateGlobalValuesTable(GlobalValuesTable currentGlobalValuesTable)
{
this.ObjectContext.GlobalValuesTables.AttachAsModified(currentGlobalValuesTable,
this.ChangeSet.GetOriginal(currentGlobalValuesTable));
}
正是 THIS 引发了错误,而且它实际上并没有更新。我也试过用这段代码代替它......
public void UpdateGlobalValuesTable(GlobalValuesTable d)
{
var newInstanceId =
this.ObjectContext.GlobalValuesTables.Single<GlobalValuesTable>
(gId => gId.RowId == 1);
// Update the current instance ID
newInstanceId.CurrentTestInstance++;
this.ObjectContext.AcceptAllChanges();
//this.ObjectContext.GlobalValuesTables.Context.AcceptAllChanges();
//this.ObjectContext.GlobalValuesTables.Context.AttachAsModified(newInstanceId);
}
【问题讨论】:
-
如果您标记问题并正确命名问题可能会有所帮助。然后,了解该技术的人可能会真正阅读它。这是不是 ado.net 问题,这是实体框架问题。也与silverlight没有任何关系。
标签: c# silverlight ado.net