7.3 数据注解特性之ConcurrencyCheck特性【Code-First系列】
ConcurrencyCheck特性可以应用到领域类的属性中。当EF执行更新操作的时候,Code-First将列的值放在where条件语句中,你可以使用这个CurrencyCheck特性,使用已经存在的列做并发检查,而不是使用单独的TimeStamp列来做并发检查。
看下面的代码:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { [Table("StudentInfo")] public class Student { [Key] [Column(Order=)] public int StudentKey1 { get; set; } [Key] [Column(Order=)] public int StudentKey2 { get; set; } [Column("Name",TypeName="ntext")] [MaxLength()] [ConcurrencyCheck] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StdId { get; set; } [ForeignKey("StdId")] public virtual Standard Standard { get; set; } [Timestamp] public byte[] RowVersion { get; set; } } }
然后我们来修改一下Main函数测试的代码:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF2 { class Program { static void Main(string[] args) { Student stuModel1 = null; Student stuModel2 = null; using (var db = new DbContextClass()) { stuModel1 = db.Students.Where(s => s.StudentKey1 == && s.StudentKey2 == ).Single(); } using (var db = new DbContextClass()) { stuModel2 = db.Students.Where(s => s.StudentKey1 == && s.StudentKey2 == ).Single(); } stuModel1.StudentName = "Test Only For one"; stuModel2.StudentName = "Test Only For twos"; try { using (var db = new DbContextClass()) { db.Entry(stuModel1).State = EntityState.Modified; db.SaveChanges(); } } catch (DbUpdateConcurrencyException ex) { Console.WriteLine(ex.Message); } try { using (var db = new DbContextClass()) { db.Entry(stuModel2).State = EntityState.Modified; db.SaveChanges(); } } catch (DbUpdateConcurrencyException ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } } }
然后错误并发消息是:
exec sp_executesql N'UPDATE [dbo].[StudentInfo] SET [StudentName] = @, [StdId] = @ WHERE ((([StudentKey1] = @) AND ([StudentKey2] = @)) AND ([StudentName] = @)) nvarchar(),@ nvarchar()',@0=N'Test Only For one',@1=1,@2=1,@3=1,@4=N'Test Only For one'
请注意:
Note that TimeStamp attribute can only be applied to a single byte array property in a class, whereas ConcurrencyCheck attribute can be applied to any number of properties with any datatype.
TimeStamp特性只能够被用到单字节属性的类中,但是ConcurrencyCheck特性可以被用到任何数量,任何类型的属性中。
7.3 数据注解特性之ConcurrencyCheck特性【Code-First系列】的更多相关文章
- 数据注解和验证 – ASP.NET MVC 4 系列
不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-i ...
- 7.7 数据注解特性--Table
大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
随机推荐
- Hbuilder开发HTML5 APP之创建子页面
折腾了好久,终于看明白怎么创建了: 1.创建个html5的mui页面,在其初始化方法中: mui.init({ subpages:[{ id:"list", url: ...
- 通过远程 http API 来控制 lnmp 环境的重启perl脚本
#!/usr/bin/perl use DBD::mysql; use strict; use warnings; use DBI; use utf8; binmode(STDOUT, ':encod ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- SEO:让搜索引擎对你的网站更有亲和力(译)
人可以通过查看网站信息了解网站的内容,但是搜索引擎只对标签感兴趣,对内容的识别能力是很低的,如何让蜘蛛通过标签认识你的文章内容呢~ 原文网址:http://schema.org/docs/gs.htm ...
- shell使用攻略
shell 是什么 ~ $ ls /bin/*sh /bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh 是什么 kernel shell 命令 ...
- 部分安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法
前端JS中使用XMLHttpRequest 2上传图片到服务器,PC端和大部分手机上都正常,但在少部分安卓手机上上传失败,服务器上查看图片,显示字节数为0.下面是上传图片的核心代码: HTML < ...
- 记一个简单的保护if 的sh脚本
真是坑爹,就下面的sh,竟然也写了很久! if [ `pwd` != '/usr/xx/bin/tomcat' ] then echo "rstall is not allowed in c ...
- promise实现原理
先看的这篇有问题的文章 花了很长时间研究这篇文章,卡在实现串行Promise那儿了,一直看不明白.就在刚才,发现这篇文章是错的,在第一次用setTimeout( ,0)那儿就错了.虽然用setTime ...
- Math的三个将小数值舍入为整数方法
舍入规则: Math.ceil():执行向上舍入,即它总是将数值向上舍入为最接近的整数: Math.floor():执行向下舍入,即它总是将数值向下舍入为最接近的整数: Math.round():执行 ...