简体   繁体   English

ADO.NET更新不起作用

[英]ADO.NET Update not working

Could one of you lovely people shed some light as to why this code doesn't work or a better/correct way of performing it... 你们其中一个可爱的人能否就此代码为什么不起作用或执行它的更好/正确方式阐明一些信息...

CandidDomainService db = new CandidDomainService();

GlobalValuesTable newInstanceId =
    db.GetGlobalValuesTables().Single<GlobalValuesTable>(gId => gId.RowId == 1);

newInstanceId.CurrentTestInstance++;
db.UpdateGlobalValuesTable(newInstanceId);

It's throwing a null exception at db.Update . 它在db.Update null异常。 I'm using the generated add, update, remove and get. 我正在使用生成的添加,更新,删除和获取。 The exception is described as being thrown when no changes have been made to the entity model. 当未对实体模型进行任何更改时,将异常描述为抛出。

Thanks! 谢谢!

EDIT: 编辑:

This is the update code generated... 这是生成的更新代码...

    public void UpdateGlobalValuesTable(GlobalValuesTable currentGlobalValuesTable)
    {
        this.ObjectContext.GlobalValuesTables.AttachAsModified(currentGlobalValuesTable, 
            this.ChangeSet.GetOriginal(currentGlobalValuesTable));
    }

And it is THIS that is throwing the error, what's more it does not actually update. 这是引发错误的原因,更何况它实际上并未更新。 I also tried this code in place of it... 我也尝试用此代码代替它...

    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);
    }

As with most things, I would check that it exists before calling it. 与大多数事情一样,在调用它之前,我会先检查它是否存在。

if (newInstanceId)
{
newInstanceId.CurrentTest Instance++;
db.UpdateGlobalValuesTable(newInstanceId);
}
else 
{
//Do something here to notify you.
}

That should at least get you into some better debugging. 至少应该使您能够进行更好的调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM