DataAnnotations in Code-First:

EF Code-First provides a set of DataAnnotation attributes, which you can apply to your domain classes and properties. DataAnnotation attributes override default Code-First conventions.System.ComponentModel.DataAnnotations includes attributes that impacts on nullability or size of the column. System.ComponentModel.DataAnnotations.Schema namespace includes attributes that impacts the schema of the database.

Note: DataAnnotations only give you a subset of configuration options. Fluent API provides a full set of configuration options available in Code-First.

System.ComponentModel.DataAnnotations Attributes:

Attribute Description
Key Mark property as EntityKey which will be mapped to PK of the related table.
Timestamp Mark the property as a non-nullable timestamp column in the database.
ConcurrencyCheck ConcurrencyCheck annotation allows you to flag one or more properties to be used for concurrency checking in the database when a user edits or deletes an entity.
Required The Required annotation will force EF (and MVC) to ensure that property has data in it.
MinLength MinLength annotation validates property whether it has minimum length of array or string.
MaxLength MaxLength annotation is the maximum length of property which in turn sets the maximum length of a column in the database
StringLength Specifies the minimum and maximum length of characters that are allowed in a data field.

System.ComponentModel.DataAnnotations.Schema Attributes:

Attribute Description
Table Specify name of the DB table which will be mapped with the class
Column Specify column name and datatype which will be mapped with the property
Index Create an Index for specified column. (EF 6.1 onwards only)
ForeignKey Specify Foreign key property for Navigation property
NotMapped Specify that property will not be mapped with database
DatabaseGenerated DatabaseGenerated attribute specifies that property will be mapped to computed column of the database table. So, the property will be read-only property. It can also be used to map the property to identity column (auto incremental column).
InverseProperty InverseProperty is useful when you have multiple relationships between two classes.
ComplexType Mark the class as complex type in EF.

Learn about each DataAnnotation attributes in the next sections.

Entity Framework Code-First(9):DataAnnotations的更多相关文章

  1. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  2. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  3. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  4. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  5. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  6. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  7. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  8. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

  9. Entity Framework Code First (六)存储过程

    声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...

  10. Entity Framework Code First (五)Fluent API - 配置关系

    上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...

随机推荐

  1. Spark Structured Streaming框架(3)之数据输出源详解

    Spark Structured streaming API支持的输出源有:Console.Memory.File和Foreach.其中Console在前两篇博文中已有详述,而Memory使用非常简单 ...

  2. 机器学习相关知识整理系列之一:决策树算法原理及剪枝(ID3,C4.5,CART)

    决策树是一种基本的分类与回归方法.分类决策树是一种描述对实例进行分类的树形结构,决策树由结点和有向边组成.结点由两种类型,内部结点表示一个特征或属性,叶结点表示一个类. 1. 基础知识 熵 在信息学和 ...

  3. MySQL5.7.9(GA)的安装

    1.解压ZIP文件到安装目录: 2.进入到bin目录,试运行mysqld --console,查看可能的出错信息,安装相应的辅助软件,如.net V4.0等: 3.编辑my.ini文件,关键内容如下: ...

  4. MySql-rules

    1.mySql基础 USE day15; -- 创建表 CREATE TABLE teacher( id INT, NAME VARCHAR() ) -- 查看所有表 SHOW TABLES; DES ...

  5. Spring Boot- 设置拦截打印日志

    import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspe ...

  6. BZOJ 2243 [SDOI2011]染色:树剖【维护路径上颜色段】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 题意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径 ...

  7. 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】

    迭代版本用的是二叉树的DFS,中的root->right->left +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  8. mongodb与mysql的命令对比

    mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...

  9. windows 7下mingw+msys编译ffmpeg

      windows 7下mingw+msys编译ffmpeg   1-->下载安装MingW,mingw-get-inst-20120426.exe  http://sourceforge.ne ...

  10. OSS阿里云文件上传 demo。

    所需jar包: aliyun-openservices-1.2.3.jar jdom-1.1.jar commons-codec-1.4.jar commons-logging-1.1.1.jar g ...