原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-code-first.aspx

ConcurrencyCheck属性可以应用于域类的属性。 当EF执行表的update命令时,Code First会在“where”子句中使用列的值。 当你想要使用现有列进行并发检查时,可以使用ConcurrencyCheck属性,而不是用并发的单独时间戳列。

请看以下示例:

using System.ComponentModel.DataAnnotations;

public class Student
{
public Student()
{ } public int StudentId { get; set; } [ConcurrencyCheck]
public string StudentName { get; set; }
}

如上例所示,ConcurrencyCheck属性应用于Student类的现有StudentName属性。

因此,Code-First将在update命令中包含StudentName列,以检查乐观并发。

exec sp_executesql
N'UPDATE [dbo].[Students]
SET [StudentName] = @0
WHERE (([StudentId] = @1) AND ([StudentName] = @2))',
N'@0 nvarchar(max) ,
@1 int,
@2 nvarchar(max) ',
@0=N'Steve',
@1=1,
@2=N'Bill'
go

注意,TimeStamp属性只能应用于类中的单字节数组属性,而ConcurrencyCheck属性可以应用于具有任何数据类型的任意数量的属性。

有关乐观锁和悲观锁、示例等请参考园友文章:http://www.cnblogs.com/Gyoung/archive/2013/01/18/2866649.html

【译】第12节---数据注解-ConcurrencyCheck的更多相关文章

  1. 【译】第20节---数据注解-InverseProperty

    原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...

  2. 【译】第19节---数据注解-NotMapped

    原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...

  3. 【译】第18节---数据注解-ForeignKey

    原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...

  4. 【译】第17节---数据注解-Column

    原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...

  5. 【译】第16节---数据注解-Table

    原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...

  6. 【译】第15节---数据注解-StringLength

    原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...

  7. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  8. 【译】第13节---数据注解-Required

    原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...

  9. 【译】第11节---数据注解-TimeStamp

    原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...

随机推荐

  1. Linux下实现免密登录

    过程如下: 1.Linux下生成密钥 通过命令”ssh-keygen -t rsa“ 2.1 通过ssh-copy-id的方式 命令: ssh-copy-id -i ~/.ssh/id_rsa.put ...

  2. 转:【专题三】自定义Web服务器

    前言: 经过前面的专题中对网络层协议和HTTP协议的简单介绍相信大家对网络中的协议有了大致的了解的, 本专题将针对HTTP协议定义一个Web服务器,我们平常浏览网页通过在浏览器中输入一个网址就可以看到 ...

  3. 深入剖析Kubernetes k8s

    深入剖析Kubernetes k8s 毫无疑问,Kubernetes 已经成为容器领域当之无愧的事实标准.除了 Google.Microsoft 等技术巨擘们在容器领域里多年的博弈外,国内的 BAT. ...

  4. rabbitMq 教程

    https://github.com/401Studio/WeekLearn/issues/2 目录 RabbitMQ 概念 exchange交换机机制 什么是交换机 binding? Direct ...

  5. vue路由3:子路由

    <div id="app"> <div> <router-link to="/">首页</router-link> ...

  6. AtCoder Beginner Contest 066 B - ss

    题目链接:http://abc066.contest.atcoder.jp/tasks/abc066_b Time limit : 2sec / Memory limit : 256MB Score ...

  7. Python中*args和**kwargs 的简单使用

    # 在函数定义中使用*args和kwargs传递可变长参数. *args用作传递非命名键值可变长参数列表(位置参数); kwargs用作传递键值可变长参数列表# *args表示任何多个无名参数,它是一 ...

  8. Django框架----Object Relational Mapping(ORM)

    Django中的ORM Django项目使用MySQL数据库 1. 在Django项目的settings.py文件中,配置数据库连接信息: DATABASES = { "default&qu ...

  9. Hybrid App中原生页面 VS H5页面(分享)

    本文部分转自  http://www.jianshu.com/p/00ff5664e000 现有3类主流APP,分别为:Web App.Hybrid App(混合模式移动应用,Hybrid有“混合的” ...

  10. oracle goldengate 远程捕获和投递

    很早之前,OGG只支持部署在数据库主机上,这叫本地化部署.而现在OGG支持远端部署,即OGG软件不安装在数据库主机上,而是安装在单独的机器上,负责数据抽取和投递. 这样做的好处: l 易于管理 - 在 ...