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

EF6/EFCore Code-First Timestamp SQL Server的更多相关文章

  1. Code First ef SQL Server 版本不支持数据类型“datetime2”

    When calling DbContext.SaveChanges, I get a DbUpdateException:An unhandled exception of type 'System ...

  2. 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005

    https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...

  3. TIMESTAMP类型字段在SQL Server和MySQL中的含义和使用

    公众号上转的满天飞的一篇文章,MySQL优化相关的,无意中瞄到一句“尽量使用TIMESTAMP而非DATETIME”,之前对TIMESTAMP也不太熟悉,很少使用,于是查了一下两者的区别. 其实,不管 ...

  4. Integrating .NET Code and SQL Server Reporting Services

    SQL Server Reporting Services versions 2000 and 2005 (SSRS) has many powerful features. SSRS has a w ...

  5. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  6. SQL Server Database Backup and Restore in C#

    SQL Server Database Backup and Restore in C# Syed Noman Ali Shah,                          7 Feb 201 ...

  7. SQL Server on Linux: How? Introduction: SQL Server Blog

    SQL Server Blog Official News from Microsoft’s Information Platform https://blogs.technet.microsoft. ...

  8. .NET Core EF框架使用SQL server 2008数据库分页问题:Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement

    一. 问题 最近.Net Core程序部署到服务器,采用EF6.本地数据库是SQL server 2016,服务器数据库安装的是SQL server 2008 R2,在用到分页查询时报错如下: { & ...

  9. sql server时间戳timestamp

    sql server时间戳timestamp 在SQL Server中联机丛书是这样说的: SQL Server timestamp 数据类型与时间和日期无关.SQL Server timestamp ...

  10. 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期

    腾讯云图片鉴黄集成到C#   官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...

随机推荐

  1. Vue——生命周期

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 004. github使用

    github的使用 GitHub是一个git版本库的托管服务,GitHub是目前全球最大的软件仓库,拥有上班玩的开发者用户,也是软件开发和寻找资源的最佳途径,GitHub不仅可以托管各种git版本参控 ...

  3. kubeadm部署高可用版Kubernetes1.21[基于centos7.6]

    1. 基础环境规划: 主机名 IP地址 节点说明 k8s-node01 192.168.1.154 node1节点 k8s-node02 192.168.1.155 node2节点 master01 ...

  4. 在 Flask 项目中配置 Session:简明指南

    在 Flask 项目中配置 Session:简明指南 本文介绍如何在 Flask 项目中配置会话 1. Flask 内置会话 Flask 自带会话管理功能,使用客户端 Cookie 存储会话数据.默认 ...

  5. manjaro安装错误的显卡驱动导致无法开机(解决办法)

    手欠的我 最近发觉我的manjaro有时候开机会卡死在clean ....这个地方,然后我去看了下系统的日至,发现是nvidia的一个报错,我也不太懂什么意思 然后我就自己动手想改改显卡驱动(从pri ...

  6. 告别Word,用Python打造你的专业简历!

    今天给大家介绍下一个在纯 python 中构建简历的实用工具,工具的连接地址https://github.com/koek67/resume-builder/blob/main/readme.md 用 ...

  7. Linux扩展篇-shell编程(五)-流程控制(四)-while语句

    基本语法: while [ condition ] do statement done 或 while [ condition ]; do statement done 注意事项: 实践:

  8. windows 命令行调整跃点数

    先用 route print -4 命令找到接口号: 接口列表10...00 ff 51 c4 53 b4 ......TAP-Windows Adapter V918...18 c0 4d 29 5 ...

  9. 思迅Pay PC ,WIN7 ,KB3042058

    思迅Pay PC ,WIN7, COM , 串口 . 577,驱动. WIN7 SHA256补丁,KB3033929,https://www.microsoft.com/zh-CN/download/ ...

  10. redis数据类型篇

    redis数据类型官网资料,https://redis.io/docs/manual/data-types/ 生产环境下的redis实况图 超哥这个redis实例里,db0库有140万个key. 1. ...