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

EF 6 Code-First系列文章目录:

在EF 6和EF Core中,ConcurrencyCheck可以应用于实体的一个或者多个属性上面。当属性被标识了这个特性,在生成的数据表中的相应的列,就会用来做乐观检查.

using System.ComponentModel.DataAnnotations;

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

在上面的例子中,ConcurrencyCheck特性应用在Student实体的StudentName属性上,所以EF将会在更新的时候,包含StudentName列,用于乐观检查,看看下面的例子:

using(var context = new SchoolContext())
{
var std = new Student()
{
StudentName = "Bill"
}; context.Students.Add(std);
context.SaveChanges(); std.StudentName = "Steve";
context.SaveChanges();
}

上面的代码例子,将会在更新的时候,生成这样的语句【Where条件中】:

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特性只能用在单独的byte数组属性上,而ConcurrencyCheck特性可以用在任何数据类型的的任何数量的属性上面。

9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】的更多相关文章

  1. 7.3 数据注解特性之ConcurrencyCheck特性【Code-First系列】

    ConcurrencyCheck特性可以应用到领域类的属性中.当EF执行更新操作的时候,Code-First将列的值放在where条件语句中,你可以使用这个CurrencyCheck特性,使用已经存在 ...

  2. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  3. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  4. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  5. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

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

  6. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

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

  7. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  8. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

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

  9. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

随机推荐

  1. 类属性判断 hasattr getattr

  2. fmod()函数 (对浮点数取模)

    头文件:#include <math.h> fmod() 用来对浮点数进行取模(求余),其原型为:    double fmod (double x); 设返回值为 ret,那么 x = ...

  3. 如何在MacBook的以太网端口上成功运行DHCP服务器?

    我的目标是在我的MacBook以太网端口上安装一个以太网交换机,我将通过DHCP连接几个Raspberry Pi连接,每个都将运行VNC服务器进行远程访问,我希望我的互联网可以从我的MacBook的W ...

  4. Visual Studio Code-GO tasks 设置 (实现在vsc下直接编译输出的功能)

    Visual Studio Code -GO 使用过程中发现,如果要编译输出某个文件需要去cmd窗口才行,感觉特别麻烦网上一直没找到解决办法,这几天查看Visual Studio Code文档发现它提 ...

  5. DSP 知识点

    1:自定义数据段 #pragma DATA_SECTION(symbol,".section") Int32 symbol[LENGTH]; 2:前几天遇到一个问题.dsp在执行某 ...

  6. 使用MSF发现主机和端口扫描

    使用MSF发现主机和端口扫描 使用search命令查找需要的模块 MSF模块太多,记不住怎么办!!! 我们不需要记住所有模块,我们只要能找到我们想用的模块就行,平时积累使用的模块也行哦! 比如,我们通 ...

  7. [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)

    如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...

  8. mysql-ubuntu卸载安装mysql

    安装MySQL sudo apt-get install mysql-server mysql-client 查看状态 是否是运行中 sudo service mysql status 启动MySQL ...

  9. JAVA "GMT+10" 和 "GMT+0010"

    可以使用 getAvailableIDs 方法来对所有受支持的时区 ID 进行迭代.可以选择受支持的 ID 来获得 TimeZone.如果想要的时区无法用受支持的 ID 之一表示,那么可以指定自定义时 ...

  10. Web大前端面试题-Day8

    1. 说说你对作用域链的理解? 作用域链的作用是保证执行环境里 有权访问的变量和函数是有序的, 作用域链的变量只能向上访问, 变量访问到window对象即被终止, 作用域链向下访问变量是不被允许的; ...