使用Linq更新单个字段到SQL - 我可以只禁用所有并发逻辑吗?

时间:2022-09-16 06:58:57

Yes, this subject is covered on hundreds of posts all over the net, and yet I still haven't found the one that doesn't involve loading an entire entity (sometimes serialized) just to change a single field. Some had suggested that changing "Update Check" on all properties of the entities would resolve this, but so far I'm still getting ChangeConflictExceptions no matter my approach. Is there any way to just tell L2S to stop being my nanny and let me update?

是的,这个主题已经覆盖了整个网络上的数百个帖子,但我仍然没有找到一个不涉及加载整个实体(有时是序列化的)只是为了改变单个字段。有人建议在实体的所有属性上更改“更新检查”可以解决这个问题,但到目前为止,无论我的方法如何,我仍然会收到ChangeConflictExceptions。有没有办法告诉L2S不再是我的保姆让我更新?

var context = new MyDataContext();
var person = new Person() {Id = 5};
person.LastName = "Johanssen";
context.People.Attach(person);
context.SubmitChanges();

Thanks so much for your insight!

非常感谢您的见解!

James

1 个解决方案

#1


Ok, I wasn't watching Visual Studio closely enough. Even though in the designer I had changed all my fields to UpdateCheck.Never when I looked at the MyModel.desinger.cs file to see the generated entities, that attribute wasn't being added to most of the fields. When I added the attribute, the update went through and all was well.

好吧,我没有仔细观察Visual Studio。尽管在设计器中我将所有字段都更改为UpdateCheck.Never当我查看MyModel.desinger.cs文件以查看生成的实体时,该属性未被添加到大多数字段中。当我添加属性时,更新通过,一切都很顺利。

If you need to add this attribute to many fields at once, here is a VS Regular Expression replacement that will append it to all the columns in the file. I HIGHLY suggest checking the file in (or backing it up) first to make sure you don't lose all your hard work first. And if you already have the attribute set on some, it will double them up (thus the second replacement below).

如果您需要一次将此属性添加到多个字段,这里是VS正则表达式替换,它将附加到文件中的所有列。我强烈建议先检查文件(或备份),以确保不会丢失所有的辛勤工作。如果你已经在某些设置上设置了属性,它会将它们加倍(因此下面的第二个替换)。

Find and Replace (Regular Expressions on)
\[Column{.*}\)\]
[Column\1, UpdateCheck=UpdateCheck.Never )]

Find and Replace (Regular Expressions off)
UpdateCheck=UpdateCheck.Never, UpdateCheck=UpdateCheck.Never  
UpdateCheck=UpdateCheck.Never

#1


Ok, I wasn't watching Visual Studio closely enough. Even though in the designer I had changed all my fields to UpdateCheck.Never when I looked at the MyModel.desinger.cs file to see the generated entities, that attribute wasn't being added to most of the fields. When I added the attribute, the update went through and all was well.

好吧,我没有仔细观察Visual Studio。尽管在设计器中我将所有字段都更改为UpdateCheck.Never当我查看MyModel.desinger.cs文件以查看生成的实体时,该属性未被添加到大多数字段中。当我添加属性时,更新通过,一切都很顺利。

If you need to add this attribute to many fields at once, here is a VS Regular Expression replacement that will append it to all the columns in the file. I HIGHLY suggest checking the file in (or backing it up) first to make sure you don't lose all your hard work first. And if you already have the attribute set on some, it will double them up (thus the second replacement below).

如果您需要一次将此属性添加到多个字段,这里是VS正则表达式替换,它将附加到文件中的所有列。我强烈建议先检查文件(或备份),以确保不会丢失所有的辛勤工作。如果你已经在某些设置上设置了属性,它会将它们加倍(因此下面的第二个替换)。

Find and Replace (Regular Expressions on)
\[Column{.*}\)\]
[Column\1, UpdateCheck=UpdateCheck.Never )]

Find and Replace (Regular Expressions off)
UpdateCheck=UpdateCheck.Never, UpdateCheck=UpdateCheck.Never  
UpdateCheck=UpdateCheck.Never