原文链接: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. IE浏览器url中带中文报错的问题;以及各种兼容以及浏览器问题总结

    1.解决IE浏览器url带中文报错 /* encodeURI()解决IE浏览器请求url中带中文报错的问题 */ URL = encodeURI("<%=basePath%>ve ...

  2. Linux学习之文件系统常用命令(七)

    Linux文件系统常用命令 目录 df命令 du命令 fsck命令 dump2fs命令 df命令 df命令 统计文件系统的占有情况,分区用了多少空间,还剩多少空间 df [选项] [挂载点] 选项: ...

  3. 函数式编程之 Python

    上接 python 函数式编程学习笔记 参考:www.sigai.cn/ 1 函数式编程概述 前提:函数在 Python 中是⼀等对象 工具:built-in ⾼阶函数:lambda 函数:opera ...

  4. LoRaWAN 1.1 网络协议规范 - 2 LoRaWAN选项介绍

    LoRaWAN 1.1 网络协议规范 LoRaWAN 1.1 版本封稿很久了也没有完整啃过一遍,最近边啃边翻译,趁着这个机会把它码下来. 如果觉得哪里有问题,欢迎留言斧正. 翻译不易,转载请申明出处和 ...

  5. git使用经验for windows

    一.本地同步fork的最新版本 二.git命令 一.本地同步fork的最新版本 ①打开Git CMD工具,进入git的主目录 ②使用 git remote -v 查看fork的远程仓库地址 origi ...

  6. 8.9 正睿暑期集训营 Day6 C 风花雪月(DP)

    题目链接 完整比赛在这儿. 杜老师tql . 求期望要抽卡的次数,也就是求期望经历了多少不满足状态.而每个不满足的状态对答案的贡献为\(1\),所以可以直接算概率.即\(Ans=\sum_{不满足状态 ...

  7. Flask信号源码流程

    1. appcontext_pushed = _signals.signal('appcontext-pushed'# 请求app上下文push时执行 return RequestContext(se ...

  8. bootStrap中的ul导航

    <!doctype html><html > <head> <meta charset="utf-8"> <link rel= ...

  9. React系列文章:JSX生成真实DOM结点

    在上一篇文章中,我们介绍了Babel是如何将JSX代码编译成可执行代码的,随后也实现了一个自己的解析器,模拟了Babel编译的过程. 现在我们再来回顾一下,假定有如下业务代码: const style ...

  10. CF733F Drivers Dissatisfaction【链剖】【最小生成树应用】

    F. Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input s ...