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 ...
随机推荐
- FMX保存JPG格式的Stream
刚刚看以前的笔记,估计这个用的人很少 var surf:TBitmapSurface; astream:TmemoryStream; begin surf:=TbitmapSurface.Create ...
- XMPP iOS客户端实现三:登录、注册
1.创建一个单例模式来管理xmpp的连接和操作 +(XMPPManager *)share { static XMPPManager *_share=nil; static dispatch_once ...
- Windows和Linux都有的Copy-on-write技术
Windows和Linux都有的Copy-on-write技术 MySQL技术内幕Innodb存储引擎第2版 P375 SQL Server2008 实现与维护(MCTS教程)P199 LVM快照技术 ...
- 使用Unity3D的设计思想实现一个简单的C#赛车游戏场景
最近看了看一个C#游戏开发的公开课,在该公开课中使用面向对象思想与Unity3D游戏开发思想结合的方式,对一个简单的赛车游戏场景进行了实现.原本在C#中很方便地就可以完成的一个小场景,使用Unity3 ...
- 关于新加坡IT薪酬
很多朋友发邮件或留言问我关于新加坡IT薪酬的问题,由于前段时间比较忙,所以没有及时一一回复,在此表示抱歉. 新加坡IT薪酬范围大概如下(月薪,新加坡币对人民币为1:5): Junior Develop ...
- 巧用transform实现HTML5 video标签视频比例拉伸
前言 原文地址 曾几何时,项目中有碰到视频比例拉伸的需求,但是发现这个看似再普通不过的一个需求,找遍全网至今都没有找到解决方法.因为强制给video标签设置width和height的话只会将video ...
- ASP.NET MVC 从零开始 - Web.config
这篇文章是从我的 github 博客 http://lxconan.github.io 导入的. 在上一篇中,我们从零开始创建了一个非常简单的 ASP.NET MVC 应用程序.接下来,你是不是期望我 ...
- Chrome开发者工具不完全指南:(三、性能篇)
卤煮在前面已经向大家介绍了Chrome开发者工具的一些功能面板,其中包括Elements.Network.Resources基础功能部分和Sources进阶功能部分,对于一般的网站项目来说,其实就是需 ...
- iOS中多线程知识总结(二)
1.GCD GCD全称是Grand Central Dispatch,译为"强大的中枢管理器" 1)什么是任务?什么是队列? 任务和队列是GCD的核心. 任务: 执行什么操作 队列 ...
- LINQ系列:Linq to Object元素操作符
元素操作符从一个序列返回单个指定的元素. 1. DefaultIfEmpty DefaultIfEmpty操作符将一个空集合替换为包含默认的单个值的集合.在返回序列为空且又需要返回一些对象时,可以通过 ...