数据注解属性--TimeStamp特性【Code-First 系列】

TimeStamp特性可以应用到领域类中,只有一个字节数组的属性上面,这个特性,给列设定的是tiemStamp类型。在并发的检查中,Code-First会自动使用这个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)]
        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; }

    }
}

运行程序,得到的数据库是:

时间: 2024-08-02 06:57:26

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

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

原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF 6提供了Index特性,用来在特定的列上面创建索引. class Student { public int Student_ID { get; set; } public string StudentName { get; set; } [Index] public int Registration

数据注解属性--Required

Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT NULL column in a database table for a property on which we apply Required attribute. Note that it can also be used with ASP.Net MVC as a validation att

20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】

原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 Code-First系列文章目录: 1 翻译系列:什么是Code First(EF 6 Code First 系列) 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列) 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列) 4

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

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

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

数据注解特性之ConcurrencyCheck特性【Code-First系列】

ConcurrencyCheck特性可以应用到领域类的属性中.当EF执行更新操作的时候,Code-First将列的值放在where条件语句中,你可以使用这个CurrencyCheck特性,使用已经存在的列做并发检查,而不是使用单独的TimeStamp列来做并发检查. 看下面的代码: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Co

数据注解和验证 – 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

Entityframework Code First 系列之数据注释

上一篇<Entityframework Code First 系列之项目搭建>讲了搭建一个Code First的控制台项目.里面有一些内容并没有扩展出来讲,因为篇幅有限.这篇针对上面内容中实体类的定义来讲下数据注释. 来看下Company的定义: public class Company { public long Id { get; set; } [DisplayName("名称"),Required,StringLength(50)] public string Nam