LINQ:找不到或更改行 [英] LINQ: row not found or changed

查看:80
本文介绍了LINQ:找不到或更改行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不断收到此找不到行"或更改了错误"的信息.
我已经尝试将dbml属性Udate Check更改为从不",但没有结果.
对于在此问题上能获得的任何帮助,我将不胜感激.
以下是.cs文件的代码.

谢谢

MikeFIF
代码CS文件:

Hi,

I keep getting this Row not found or changed error.
I''ve already tried changing the dbml property Udate Check to "never", without results.
I would greatly appreciate any help I can get on this matter.
Below is the code of the .cs file.

Thanx

MikeFIF
Code CS-file:

protected void Button13_Click(object sender, EventArgs e)
    {
FacturatieDataContext adm1n = new FacturatieDataContext();
    Beheren beheer3 = new Beheren();
 Beheren beheer2 = new Beheren();
//select the row
var b3h3er1 = from c in adm1n.Beherens
                      where c.Naambeheer == TextBox1.Text
                      select c;

            //Make a new beheren
            beheer3.Naambeheer = TextBox4.Text;
            
//the old beheren
            beheer2.Naambeheer = TextBox1.Text;
            
//submit the changes
adm1n.Beherens.Attach(beheer3,beheer2);
        adm1n.SubmitChanges();
    }

推荐答案

LINQ to SQL尝试查找要更新的记录,将所有列与其原始值进行比较,如果找不到任何记录,则引发异常. LINQ to SQL就是这样告诉我们,它所寻找的行已被另一个程序删除或更改,并且更新无法完成.

在此代码中,以下行可能会引发异常

LINQ to SQL tried to look for the record to updated comparing all the columns to their original values, and when no records where found, the exception was thrown. This is the way LINQ to SQL tells us that the row it was looking for was deleted or changed by another program and the update cannot be done.

In this code, the below line might throw exception

adm1n.Beherens.Attach(beheer3,beheer2);



放置try..catch块以捕获确切的异常



Put try..catch block to capture the exact exception


尝试在Linq语句的末尾添加ToList().这将强制Linq语句立即执行,并且b3h3er1 变量将不受任何更改的影响.
Try to add a ToList() at the end of the Linq statement. That will force the Linq statement to immediately be executed, and the b3h3er1 variable will not be affected by any changes.


这篇关于LINQ:找不到或更改行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆