C# 使用 Linq-to-SQL 更新记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16378775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:53:28  来源:igfitidea点击:

Updating a record using Linq-to-SQL

c#linq-to-sql

提问by Andy Link

I do my query...

我做我的查询...

var result = from u in tdc.tblUsers
             where u.UserID == userID
             select u;

and then I change the values I want to:

然后我更改我想要的值:

foreach (tblUsers u in result)
{
     //change values (and no im not changing the primary key or foreign keys)
}

then I submit changes

然后我提交更改

tdc.SubmitChanges();

When it hits submit changes, it throws exception that the row wasn't found or was changed. I am the only person using this so there's no other conflicts with accessing the db or locking. Why would it throw the ChangeConflictException? I have stepped through with debugger and the data persists all the way through the process, including the changes that I'm trying to make.

当它点击提交更改时,它会抛出该行未找到或已更改的异常。我是唯一使用它的人,因此访问数据库或锁定没有其他冲突。为什么会抛出ChangeConflictException?我已经通过调试器逐步完成,并且数据在整个过程中一直存在,包括我尝试进行的更改。

I have also tried it this way before and got the same error

我以前也试过这种方式,得到了同样的错误

tblUsers result = (from u in tdc.tblUsers
                   where u.UserID == userID
                   select u).Single();
result.Address = address;
result.Phone = phone;
tdc.SubmitChanges();

It will only ever retrieve 1 record with this query since UserIDis the primary key.

由于UserID是主键,因此它只会使用此查询检索 1 条记录。

I've done this many times and it has worked. Every example I've found is exactly what I have.

我已经这样做了很多次,并且奏效了。我找到的每个例子都正是我所拥有的。

回答by Sascha

Maybe you work with different context? Try to encapsulate it by using

也许你在不同的环境下工作?尝试使用封装它

using (myContext ctx = new myContext())
{
    var user = ctx.users.first();  
    user.name="blah";
    ctx.SubmitChanges();
}

回答by EricRRichards

Quite often, if you look at the actual SQL that Linq-to-SQL generates, you'll see that it is being really over-zealous with matching the database row that you initially retrieved. Imagine that you have a table with columns ID(PK), A, B, C. You would think that if you updated column C for a row, it should be sufficient to update the row with a matching primary key. But what often happens is that Linq-to-SQL is also trying to match on columns A and B as well. Normally, that's fine. Unless you have concurrent writes, either from multi-threading or multi-processes, and something else modifies column A or B for the record that you are trying to update. Then you get these System.Data.Linq.ChangeConflictException: Row not found or changed. errors when you call SubmitChanges() on your data context.

很多时候,如果您查看 Linq-to-SQL 生成的实际 SQL,您会发现它非常热衷于匹配您最初检索的数据库行。想象一下,您有一个包含 ID(PK)、A、B、C 列的表。您会认为,如果您为某行更新了 C 列,那么使用匹配的主键更新该行就足够了。但经常发生的是,Linq-to-SQL 也试图匹配 A 列和 B 列。通常,这很好。除非您有并发写入,无论是来自多线程还是多进程,并且其他内容修改了您尝试更新的记录的 A 列或 B 列。然后你会得到这些 System.Data.Linq.ChangeConflictException: Row not found or changed。在数据上下文中调用 SubmitChanges() 时出错。