博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7.3 数据注解特性之ConcurrencyCheck特性【Code-First系列】
阅读量:6853 次
发布时间:2019-06-26

本文共 3233 字,大约阅读时间需要 10 分钟。

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=1)]        public int StudentKey1 { get; set; }        [Key]        [Column(Order=2)]        public int StudentKey2 { get; set; }        [Column("Name",TypeName="ntext")]        [MaxLength(20)]        [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 == 1 && s.StudentKey2 == 1).Single();                }                using (var db = new DbContextClass())                {                    stuModel2 = db.Students.Where(s => s.StudentKey1 == 1 && s.StudentKey2 == 1).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] = @0, [StdId] = @1WHERE ((([StudentKey1] = @2) AND ([StudentKey2] = @3)) AND ([StudentName] = @4))',N'@0 nvarchar(20),@1 int,@2 int,@3 int,@4 nvarchar(20)',@0=N'Test Only For one',@1=1,@2=1,@3=1,@4=N'Test Only For one'

请注意:

Note that  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特性可以被用到任何数量,任何类型的属性中。

转载地址:http://xzyyl.baihongyu.com/

你可能感兴趣的文章
编译安装 nginx的http_stub_status_module监控其运行状态
查看>>
AtCoder Regular Contest 069 D
查看>>
关于sql server批量插入与更新两种解决方案
查看>>
判断 iPhone 是否插入了 SIM 卡
查看>>
PUSH
查看>>
格式话输出
查看>>
黄聪:微信小程序 服务器 TLS1.0 1TLS.2 配置详细教学!
查看>>
黄聪:360浏览器、chrome开发扩展插件教程(3)关于本地存储数据
查看>>
(转)java 中变量存储位置总结
查看>>
系统架构介绍
查看>>
oracle唯一约束
查看>>
C++获取类成员函数地址
查看>>
【MOOC手写体】王文敏教授.《人工智能原理》 第10章 机器学习的任务 Part5 C10.2 Classification 分类...
查看>>
Plot transpant lines in Matleb 在Matlab中绘制透明线条
查看>>
MsChart<1> 线性图
查看>>
C# 调试之 Debug.WriteLine()、Trace.WriteLine()
查看>>
META Header实例讲解(转)
查看>>
自动备份5天前的日志
查看>>
一个小方法解决RGBA不兼容IE8
查看>>
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
查看>>