Entity Framework Code-First(9):DataAnnotations
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的更多相关文章
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
- Entity Framework Code First (六)存储过程
声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映 ...
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
随机推荐
- python 3 并发编程之多进程 multiprocessing模块
一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...
- Docker 命令篇
Docker命令比较对,我们来慢慢学 Docker run(运行Container) 常用选项: -d Run container in background and print container ...
- GVM管理Go版本
1.为什么要安装GVM 1.1什么是GVM GVM是一个golang虚拟环境配置工具,其允许一台机器上安装多个golang版本,gvm是第三方开发的Go多版本管理工具,类似ruby里面的rvm工具.使 ...
- php设计模式课程---2、为什么会用到简单工厂设计模式
php设计模式课程---2.为什么会用到简单工厂设计模式 一.总结 一句话总结: 比如调用数据库的语句,如果调用的数据库名字改了,或者调用的数据库类型改了(比如从Mysql用到了Mysqli),那么要 ...
- Struts调用DMI
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
- MyEclipse消除frame引起的the file xxx cannot be found
因为该页面所指向的页面路径不对,便进行手动修改,修改时却出现了很烦的问题,输入一个字就弹出一个提示框“the file XXX can not be found.Please check the lo ...
- poj3177边-双连通分量
题意和poj3352一样..唯一区别就是有重边,预先判断一下就好了 #include<map> #include<set> #include<list> #incl ...
- zoj1360/poj1328 Radar Installation(贪心)
对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间 ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)
用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstri ...