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

EF 6 Code-First系列文章目录:

EF 6和EF Core都包含TimeStamp数据注解特性。它只能用在实体的byte数组类型的属性上,并且只能用在一个byte数组类型的属性上。然后在数据库中,创建timestamp数据类型的列,在更新语句中,EF API自动使用timestamp列,用于并发检查。
一个实体只能有一个时间戳列,我们看看下面的图:

using System.ComponentModel.DataAnnotations;

public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; } [Timestamp]
public byte[] RowVersion { get; set; }
}

在上面的例子中,TimeStamp特性应用于Student实体的byte[]类型的RowVersion属性上,所以,EF 将会给RowVersion创建一个timestamp数据类型:

timestamp类型的列,在更新的时候,会包含在where语句中:

using(var context = new SchoolContext())
{
var std = new Student()
{
StudentName = "Bill"
}; context.Students.Add(std);
context.SaveChanges(); std.StudentName = "Steve";
context.SaveChanges();
}

上面的代码,将会生成下面的语句:

exec sp_executesql N'UPDATE [dbo].[Students]
SET [StudentName] = @0
WHERE (([StudentId] = @1) AND ([RowVersion] = @2))
SELECT [RowVersion]
FROM [dbo].[Students]
WHERE @@ROWCOUNT > 0 AND [StudentId] = @1',N'@0 nvarchar(max) ,@1 int,@2 binary(8)',@0=N'Steve',@1=1,@2=0x00000000000007D1
go

9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

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

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

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

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

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

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

随机推荐

  1. request中获取post的json对象数据content-type=“text/plain”

    其实采用http://www.cnblogs.com/SimonHu1993/p/7295750.html中的方法一都能获取到,就是通过获取request中的流数据,拿到json数据,理论上应该适用各 ...

  2. CPU个数、CPU核心数、CPU线程数

    CPU个数.CPU核心数.CPU线程数 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能等级.CPU从早期的单核,发展到现在的双核,多核.CPU除了核心数之外,还有线程数之 ...

  3. Chameleon

    # -*- coding: utf-8 -*- """ Created on Tue Dec 18 09:55:16 2018 @author: Mark,LI &quo ...

  4. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  5. Session丢失的解决方法

    1.修改配置文件 <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:4 ...

  6. shell 环境变量

    Ubuntu系统设置的环境变量 .profile .bashrc 在 .profile中 有一段代码: if [ -d "$HOME/bin" ] ; then PATH=&quo ...

  7. python魔法方法-属性访问控制

    属性访问控制 所谓的属性访问控制就是控制点号访问属性的行为,而且不仅是类的外部,连类的内部也受控制,代码见真章,边看代码边解释: __getattr__(self, item) 定义当访问不存在的属性 ...

  8. Java 之递归删除目录

    Java 之递归删除目录 一.思想 必须从最里层的文件开始删除,使用递归删除. 二.源代码:RecursiveDeleteDirectory.java package cn.com.zfc.day01 ...

  9. ab,qps 并发连接数

    并发连接数 = pv / 统计时间 * 页面衍生连接次数 * http响应时间 * 因数 / 其他web服务器 数量 pv = 并发连接数 * 统计时间 * 其他web服务器 数量/ 页面衍生连接次数 ...

  10. JVM笔记(一)数字在JVM中的表示

    数字在JVM中的表示 一.整数在JVM中的表示 1.在Java虚拟机中,整数有四种表示: byte:8位        short:16位     int:32位     long:64位 2.原码. ...