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. Silverlight和WPF中DataContractJsonSerializer对时间的处理差异

    原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com Silverlight脱胎于WPF,他们的行为不完全并不完全相同,DataContractJsonSerializ ...

  2. 浅析MSIL中间语言——基础篇

    一.开篇 研究MSIL纯属于个人喜好,说在前面MSIL应用于开发的地方很少,但是很大程度上能够帮着我们理解底层的原理,这是我了解MSIL的主要原因.托管代码表示应用程序的方法的功能,它们以微软的中间语 ...

  3. Qcon会议之所见所想

    作为普通码农一枚,Qcon是俺参与过的最高级的技术大会了.大会共历时三天,因为俺第二天就得赶火车休个五一大长假,所以只参加了第一天4/25号的会议(其他俩天自然有其他同事会去观摩),不过第一天的会议有 ...

  4. 【玩转单片机系列002】 如何使用STM32提供的DSP库进行FFT

    前些日子,因为需要在STM32F103系列处理器上,对采集的音频信号进行FFT,所以花了一些时间来研究如何高效并精确的在STM32F103系列处理器上实现FFT.在网上找了很多这方面的资料做实验并进行 ...

  5. Spark的持久化简记

    摘要: 1.spark 提供的持久化方法 2.Spark的持久化级别 3.如何选择一种最合适的持久化策略 内容: 1.spark 提供的持久化方法 如果要对一个RDD进行持久化,只要对这个RDD调用c ...

  6. Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结

    Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结 1. Sql语言应该得到更大的范围的应用,1 1.1. 在小型系统项目中,很适合存储过程写业务逻辑2 1.2. 大型 ...

  7. Content-Type List

    Content-Type List Description of Data Content Typical Filename Extensions MIME type/subtype       Te ...

  8. Exception:HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    主要错误信息如下: HTTP Status 500 - org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  9. 修改input框默认黄色背景

    input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill { -webkit-box-shadow: 0 0 ...

  10. UpdateException-更新条目时出错分析

    不屁话1张图搞定: 我是这个错: 2015-03-27 00:25:00,493 [9] ERROR log - System.Data.Entity.Infrastructure.DbUpdateE ...