原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-in-code-first.aspx

MaxLength

MaxLength属性可以应用于域类的字符串或数组类型属性。 EF Code First将设置MaxLength属性中指定的列的大小。 请注意,它也可以与ASP.Net MVC一起用作验证属性。

请看以下示例:

using System.ComponentModel.DataAnnotations;

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

如上面的代码所示,我们已经将MaxLength特性应用于StudentName属性。 所以,Code-First将在Student表中创建一个nvarchar(50)列Student Name,如下所示:

如果将值设置为大于指定大小,则EF还会验证MaxLength属性的属性值。

例如,如果您设置了超过50个字符长StudentName,则EF将抛出EntityValidationError。

MinLength

MinLength属性是一个验证属性。 它对数据库模式没有影响。 如果在MinLength属性中设置小于指定长度的字符串或数组属性的值,则EF将抛出EntityValidationError。

MinLength属性也可以与MaxLength属性一起使用,如下所示:

using System.ComponentModel.DataAnnotations;

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

在上面的例子中,StudentName不能少于2个字符,不能超过50个字符。

【译】第14节---数据注解-MaxLength/MinLength的更多相关文章

  1. 【译】第15节---数据注解-StringLength

    原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code- ...

  2. 【译】第20节---数据注解-InverseProperty

    原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...

  3. 【译】第19节---数据注解-NotMapped

    原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...

  4. 【译】第18节---数据注解-ForeignKey

    原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...

  5. 【译】第17节---数据注解-Column

    原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...

  6. 【译】第16节---数据注解-Table

    原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...

  7. 【译】第13节---数据注解-Required

    原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...

  8. 【译】第12节---数据注解-ConcurrencyCheck

    原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...

  9. 【译】第11节---数据注解-TimeStamp

    原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...

随机推荐

  1. 20165215 2017-2018-2 《Java程序设计》第九周学习总结

    20165215 2017-2018-2 <Java程序设计>第九周学习总结 教材学习内容总结 URL类 URL 类是 java.net 包中的一个重要的类,使用 URL 创建对象的应用程 ...

  2. 面试必问之JVM篇

    前言 只有光头才能变强 JVM在准备面试的时候就有看了,一直没时间写笔记.现在到了一家公司实习,闲的时候就写写,刷刷JVM博客,刷刷电子书. 学习JVM的目的也很简单: 能够知道JVM是什么,为我们干 ...

  3. 四轴飞行器1.7 NRF24L01P无线通讯和改进型环形缓冲(转)

    源: 四轴飞行器1.7 NRF24L01P无线通讯和改进型环形缓冲

  4. qt裁剪

    1. qt裁剪是什么,qt有哪些模块 1.1 2. windows消息机制,qt的signal slot,android/ios消息机制?

  5. Java连接数据库 #04# Apache Commons DbUtils

    索引 通过一个简单的调用看整体结构 Examples 修改JAVA连接数据库#03#中的代码 DbUtils并非是什么ORM框架,只是对原始的JDBC进行了一些封装,以便我们少写一些重复代码.就“用” ...

  6. 安装虚拟环境virtualenvwrapper和django

    以下操作在windows平台进行 1.安装虚拟环境virtualenvwrapper 首先需要安装python管理工具pip,安装完python3.7之后自带了pip,可通过where pip查看管理 ...

  7. Introduction to the Standard Directory Layout

    Having a common directory layout would allow for users familiar with one Maven project to immediatel ...

  8. PyCharm‘s Project Deployment

    当在本地写完项目,部署到服务器上调试的时候,难免会碰到代码的修修改改,但由于项目在服务器上,修改起来相对麻烦.各路大神或许有自己的方法去解决.这篇博客演示利用PyCharm的Deployment功能, ...

  9. P4890 Never·island(dp)

    P4890 Never·island 求门开的最小时间,其实也就是求门关的最大时间. 坐标这么大....显然坐标要离散化 离散化排序后,我们发现x轴被这些点划分成若干条线段$(l,r)$,并且有4种情 ...

  10. Mybatis的Mapper接口方法不能重载

    今天给项目的数据字典查询添加通用方法,发现里边已经有了一个查询所有数据字典的方法 List<Dict> selectDictList(); 但我想设置的方法是根据数据字典的code查询出所 ...