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 socket 编程
Python 3 socket编程 一 客户端/服务器架构 互联网中处处是C/S架构 1.C/S结构,即Client/Server(客户端/服务器)结构 2.在互联网中处处可见c/s架构 比如说浏览器 ...
- CSS3图片悬停放大动画
在线演示 本地下载
- case 练习
#!/bin/bash RED_COLOR="\E[1;31m" GREEN_COLOR="\E[1;32m" YELLOW_COLOR="\E[1; ...
- 使用mongify将sqlserver数据导入到mongodb
最近需要将sqlserver数据导入到mongodb中,在github上搜了一圈,发现两个项目有点适合 mongify sql2mongodb 先试了下sql2mongodb(有个好名字果然有好处啊) ...
- 自顶向下归并排序(Merge Sort)
一.自顶向下的归并排序思路: 1.先把数组分为两个部分. 2.分别对这两个部分进行排序. 3.排序完之后,将这两个数组归并为一个有序的数组. 重复1-3步骤,直到数组的大小为1,则直接返回. 这个思路 ...
- docker仓库及数据卷
docker help rmi, 删除本地镜像 docker run -it --name=centos centos:latest /bin/sh --name的选项可以方便我们以后引用此imag ...
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
- IDEA 加载Eclipse项目
- Sed 命令详解 正则表达式元字符
1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏幕.具体过程如下 ...
- 创建第一个Servlet并定制Sevlet模板
我们已经为eclipse配好了Tomcat服务器,创建了Web工程,现在是时候该创建一个Servlet向世界问好了! 第一步:创建一个Web工程,选中”src“文件夹 -->单击右键,鼠标移到” ...