LINQ to SQL:并发解析 [英] LINQ to SQL: Concurrency resolution

查看:40
本文介绍了LINQ to SQL:并发解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将此LINQ转换为SQL:

Given this LINQ to SQL:

using (var db = Database.Context)
{
    var root = (from post in db.Post
                where post.Id == rootPostId
                select post).Single();

    root.LastActivityUtc = DateTime.UtcNow;

    db.SubmitChanges();
}

如果使用相同的rootPostId再次调用相同的方法(此代码所在的位置)并发更改同一记录,将会发生什么情况?会抛出异常吗?

What will happen if the same record is concurrently being changed by another call to the same method (where this code lives) with the same rootPostId? Will an exception be thrown?

在这样的事件(并发冲突)中,我想通过简单地放弃更改来处理它,以便仅提交对LastActivityUtc的一次更新,而不是同时提交两次,而无论如何更新都将具有相同的值.

In such an event--concurrency conflict--I'd like to handle it by simple discarding the change so that just one update to LastActivityUtc is submitted instead of both, which will probably have the same value anyway.

推荐答案

通过捕获使用 RefreshMode .KeepChanges您将保留客户端对象的所有更改,其他字段中其他用户的更改将被合并.

With RefreshMode.KeepChanges you will keep all changes of your client objects, and the changes from other users on other fields will be merged.

推荐文章:

  • Optimistic Concurrency Overview
  • LINQ To SQL Samples - Optimistic Concurrency
  • Resolve Concurrency Conflicts by Merging with Database Values (LINQ to SQL)

这篇关于LINQ to SQL:并发解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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