数据注解特性之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=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] = @1
WHERE ((([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 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特性可以被用到任何数量,任何类型的属性中。

时间: 2024-08-24 02:42:29

数据注解特性之ConcurrencyCheck特性【Code-First系列】的相关文章

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

       不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网络的信息都是不可信任的.        当在 ASP.NET MVC 设计模式上下文中谈论验证时,主要关注的是验证模型的值.ASP.NET MVC 验证特性可以帮助我们验证模型值,且这样验证特性是可扩展的,所以我们可以采用任意想要的方式构建验证模式,默认方法是一种声明式验证,即数据注解特性.    

数据注解特性--Table

大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被用到类中,Code--First默认的约定是使用类名称为我们创建表名,Table特性可以重写这个约定,只要我们指定名字,EF就会根据Table属性里面的名字,为我们创建数据表名称. 我们看一下下面的代码吧: using System; using System.Collections.Generic

9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】

9.11 翻译系列:数据注解特性之--Timestamp[EF 6 Code-First系列] 原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-first.aspx EF 6和EF Core都包含TimeStamp数据注解特性.它只能用在实体的byte数组类型的属性上,并且只能用在一个byte数组类型的属性上.然后在数据库中,创建timestam

EF CodeFirst数据注解特性详解

数据注解特性是.NET特性,可以在EF或者EF Core中,应用于实体类上或者属性上,以重写默认的约定规则. 在EF 6和EF Core中,数据注解特性包含在System.ComponentModel.DataAnnotations命名空间和System.ComponentModel.DataAnnotations.Schema命名空间下. 这些特性不仅仅适用于EF,同样适用于ASP.NET MVC以及数据控件. System.ComponentModel.DataAnnotations.Sch

Entity Framework(三):使用特性(数据注解)创建表结构

一.理解Code First及其约定和配置 传统设计应用的方式都是由下而上的,即我们习惯优先考虑数据库,然后使用这个以数据为中心的方法在数据之上构建应用程序.这种方法非常适合于数据密集的应用或者数据库很可能包含多个应用使用的业务逻辑的应用.对于这种应用,如果要使用EF的话,我们必须使用Database First方式. 设计应用的另一种方法就是以领域为中心的方式(领域驱动设计DDD).DDD是一种由上而下的方式,我们通过从实现应用所需要的领域模型和实体的角度思考,从而开始设计应用.数据库很少用来

数据注解和验证

①利用数据注解进行验证 ②创建自定义的验证逻辑 ③模型元数据注解的用法 ①先创建数据源 1,创建我们的Model  Order 2,创建控制器带EF 选择模型为Order 当你运行的时候会报错,需要代码迁移    code First 更新数据库 这篇文章可解决这个问题. 3,添加,不做处理的时候,显示的是这个 ②验证注解的使用 2.1自定义错误提示信息及其本地化 2.2 注解的后台原理 ASP.NET MVC 的验证特性是由模型绑定器.模型元数据.模型验证器和模型状态组成的协调系统的一部分.

ASP.NET MVC5(四):数据注解和验证

前言 用户输入验证的工作,不仅要在客户端浏览器中执行,还要在服务端执行.主要原因是客户端验证会对输入数据给出即时反馈,提高用户体验:服务器端验证,主要是因为不能完全信任用户提供的数据.ASP.NET MVC框架提供了强大的验证组件帮助我们处理这些繁杂的问题. 数据验证 验证注解的使用 验证注解特性定义在命名空间System.ComponentModel.DataAnnotations中,它们提供了服务器端验证的功能,当在模型的属性上使用时,框架也支持客户端验证.常用特性简介: Required当

Asp.net MVC 数据注解与验证

数据注解特性定义在名称空间System.ComponentModel.DataAnnotations中(有些特性定义在其他名称空间中),它们提供了服务器端验证的功能,当在模型的属性上使用这些特性时,框架也支持客户端验证. 常用特性 1.Required --必填字段示例:[Required]2.StringLength --字符长度限制示例:[StringLength(16,MinimumLength=3)]3.RegularExpression --正则表达式验证示例:[RegularExpr

asp.net mvc3 数据验证(三)—自定义数据注解

原文:asp.net mvc3 数据验证(三)-自定义数据注解         前两节讲的都是asp.net mvc3预先设定的数据注解,但是系统自由的数据注解肯定不适合所有的场合,所以有时候我们需要自定义数据注解. 自定义数据注解有两种,一种是直接写在模型对象中,这样做的好处是验证时只需要关心一种模型对象的验证逻辑,缺点也是显而易见的,那就是不能重用. 还有一种是封装在自定义的数据注解中,优点是可重用,缺点是需要应对不同类型的模型. 现在我们以封装在自定义数据注解中的方法为例看下如何在asp.