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系列】的更多相关文章

  1. 数据注解和验证 – ASP.NET MVC 4 系列

           不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...

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

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

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

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

  4. 7.7 数据注解特性--Table

    大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 基于AngularJS的企业软件前端架构[转载]

    这篇是我参加QCon北京2014的演讲内容: 提纲: 企业应用在软件行业中占有很大的比重,而这类软件多数现在也都采用B/S的模式开发,在这个日新月异的时代,它们的前端开发技术找到了什么改进点呢? B/ ...

  2. .NET项目版本号的小随笔

    [题外话] 一直以来都对.NET项目中的几个版本号(AssemblyVersion.AssemblyFileVersion.AssemblyInformationalVersion)以及版本号中的Re ...

  3. MySQL 子分区

    200 ? "200px" : this.width)!important;} --> 介绍 子分区其实是对每个分区表的每个分区进行再次分隔,目前只有RANGE和LIST分区 ...

  4. “安装项目” Step By Step

    目录 一 基本操作 二 定制安装对话框 三 安装程序类 四 总结 .NET程序集因为包含了元数据,所以程序集具有自描述性.多数程序自身包含了运行所需要的全部信息,这类程序集就是我们常说的“绿色软件”. ...

  5. CoreProfiler/NanoProfiler性能调试监控系列总目录

    NanoProfiler - 适合生产环境的性能监控类库 之 基本功能篇 NanoProfiler - 适合生产环境的性能监控类库 之 大数据篇 NanoProfiler - 适合生产环境的性能监控类 ...

  6. JS实战 · 表单验证

    思路:         1.定义页面             通过表格格式化表单:             表格行都有自己的背景颜色:             单元格中的数据(文本等)用div进行封装 ...

  7. addUser

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. windows7查看最近使用记录

    1.看计算机在哪天运行过~运行了多久! C:\Windows\SchedLgU.txt 2.看你最近运行过什么程序: C:\Windows\Prefetch 3.看你最近打开过什么文件(非程序)和文件 ...

  9. Oralce 重建EM服务,OracleDBConsoleOracle

    今天打开Oracle ,想进em看看,结果发现OracleDBConsoleOracle服务老是报错: OracleDBConsoleOracle 服务因 2 (0x2) 服务性错误而停止. 搞不懂, ...

  10. Sql Server系列:流程控制语句

    T-SQL中用来编写流程控制模块的语句有:BEGIN...AND语句.IF...ELSE语句.CASE语句.WHILE语句.GOTO语句.BREAK语句.WAITFOR语句和RETURN语句. 1 B ...