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.

  1. using System.ComponentModel.DataAnnotations;
  2.  
  3. public class Student
  4. {
  5. public Student()
  6. {
  7.  
  8. }
  9. public int StudentID { get; set; }
  10.  
  11. [MaxLength()]
  12. public string StudentName { get; set; }
  13.  
  14. }

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.

  1. using System.ComponentModel.DataAnnotations;
  2.  
  3. public class Student
  4. {
  5. public Student()
  6. {
  7.  
  8. }
  9. public int StudentID { get; set; }
  10.  
  11. [MaxLength(),MinLength()]
  12. public string StudentName { get; set; }
  13.  
  14. }

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. 【Flask模板】宏的概念和基本使用

    # 宏:模板中的宏跟python中的函数类似,可以传递参数,但是不能有返回值,可以将一些经常用到的代码片段放到宏中,然后把一些不固定的值抽取出来当成一个变量.使用宏的时候,参数可以为默认值.相关示例代 ...

  2. Memory Layout of C Programs

    Memory Layout of C Programs   A typical memory representation of C program consists of following sec ...

  3. java学习进度条四

  4. java:Maven构建项目速度太慢的解决办法,以及报错Retrieving archetypes:' has encountered a problem

    如果报错信息如下: Retrieving archetypes:' has encountered a problemAn internal error occurred during:"R ...

  5. 分享知识-快乐自己:java代码 操作 solr

    POM 文件: <!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> < ...

  6. C语言小程序(八)、统计字母个数

    这么简单的程序本不应贴在这里,但每写一篇博客,积分涨10分,距离摆脱千里之外的排名又进一步,相当于刷榜了,哈哈! #include <stdio.h> #include <strin ...

  7. 小程序wxss编译错误

    控制台输入openVendor() ,清除里面的wcsc.exe,然后重启工具.

  8. 树套树Day1线段树套平衡树bzoj3196

    您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查 ...

  9. bzoj 4034: 树上操作 线段树

    题目: 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...

  10. Access中复制表

    很多时候在Access中需要复制表,或只复制结构,源表名:a: 新表名:b (经测试中Access可用)   法一:select * into b from a where 1<>1   ...