DataAnnotations - MaxLength Attribute:

MaxLength attribute can be applied to a string or array type property of a domain class. EF Code First will set the size of a column as specified in MaxLength attribute. Note that it can also be used with ASP.Net MVC as a validation attribute.

Consider the following example.

using System.ComponentModel.DataAnnotations;

public class Student
{
public Student()
{ }
public int StudentID { get; set; } [MaxLength()]
public string StudentName { get; set; } }

As you can see in the above code, we have applied MaxLength attribute to StudentName. So, Code-First will create a nvarchar(50) column StudentName in the Student table as shown below.

Entity Framework also validates the value of a property for MaxLength attribute if you set the value more than the specified size. For example, if you set more than 50 chars long StudentName then EF will throw EntityValidationError.

MinLength:

MinLength attribute is a validation attribute. It does not have an impact on the database schema. EF will throw EntityValidationError if you set a value of a string or array property less than the specified length in MinLength attribute.

MinLength attribute can also be used with MaxLength attribute as shown below.

using System.ComponentModel.DataAnnotations;

public class Student
{
public Student()
{ }
public int StudentID { get; set; } [MaxLength(),MinLength()]
public string StudentName { get; set; } }

In the above example, StudentName can not be less than 2 chars and more than 50 chars.

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

  1. Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute

    DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...

  2. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...

  3. Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

    DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...

  4. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  5. Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

    DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...

  6. Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

    DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...

  7. Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

    DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...

  8. Entity Framework Code-First(9.4):DataAnnotations - Required Attribute

    Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...

  9. Entity Framework Code-First(9.3):DataAnnotations - ConcurrencyCheck Attribute

    ConcurrencyCheck Attribute: ConcurrencyCheck attribute can be applied to a property of a domain clas ...

随机推荐

  1. 【leetcode刷题笔记】Insertion Sort List

    Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻 ...

  2. Idea 包名按树形结构展示

    Idea默认包名展示如图: 感觉这样展示,在包下面建包的时候不方便,可以在 设置按钮 里面去掉 Flatten Packages 和 Compact Empty Middle Packages,设置如 ...

  3. form表单提交信息的方式

    form表单提交信息的方法有两种,一种是get,一种是post.get提交的数据是在地址栏上提交,一般隐私数据不会选择这样方式.地址栏上只能提交字符. 如果使用的是post提交,那么信息都在消息正文中 ...

  4. Tomcat_异常_02_IOException while loading persisted sessions: java.io.EOFException

    异常原因: EOFException表示输入过程中意外地到达文件尾或流尾的信号,导致从session中获取数据失败. 这是由于tomcat上次非正常关闭时有一些活动session被持久化(表现为一些临 ...

  5. linux命令学习笔记(35):ln 命令

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在 不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要 ...

  6. 判断CPU是大端还是小端

    #include "stdafx.h" #include <iostream> using namespace std; /* #大端模式(Big_endian):字数 ...

  7. 洛谷 P1187 3D模型

    题目描述 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上的).几个典型的城市模 ...

  8. 转 Django根据现有数据库,自动生成models模型文件

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  9. JS图表工具 ---- Highcharts

    Highcharts 是一个用纯 JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是 web 应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用. Hi ...

  10. docker 局域网仓库(registry)

    sudo docker pull daocloud.io/registry 安装仓库(registry)  使用daocloud/aliyun镜像吧,官网仓库真心没法用   sudo docker r ...