原文: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. linux 下 tomcat 运行报错 Broken pipe

    linux 下 tomcat 运行报错 Broken pipe 感谢:http://hi.baidu.com/liupenglover/blog/item/4048c23ff19f1cd67d1e71 ...

  2. Python HTMLTestRunner 学习

    HTMLTestRunner 是 基于 unittest 单元测试的 HTML报告 的一个第三库   安装: 1. 安装:下载HTMLTestRunner.py文件:地址http://tungwaiy ...

  3. python 之xml.etree.ElementTree

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...

  4. 【开源】EasyFlash 新年发布 V4.0 beta 版,完全重写(转)

    [开源]EasyFlash 新年发布 V4.0 beta 版,完全重写 EasyFlash V4.0 beta [开源]嵌入式闪存库 EasyFlash for STM32,支持Env和IAP

  5. selenium:2.selenium 键盘事件模拟

    今晚不想加班,于是赶紧回来看看书,不知道今天怎么就突然特别想玩文明五,但是又要克制自己咯,所以还是看看书吧,干的事情有: 1.下了selenium的小工具:FireBug/FirePath. 2.确定 ...

  6. curl 异步捉取数据类

    <?php class RequestLib { /** * GET 请求 * @param string $url */ public static function http_get($ur ...

  7. 一、初始PS软件

     PS的介绍 Adope Photoshop,简称“PS”,是由Adope Systems开发和发行的图像处理软件. Photoshop主要处理以像素所构成的数字图像,使用其众多的编修与绘图工具,可以 ...

  8. python简说(四)字符串

    s='.abc.abc.BCD,abc'# new_s = s.strip('.') #默认去掉字符串两边的空格和换行符# print(s.rstrip()) #去掉右边的# print(s.lstr ...

  9. 20155201 网络攻防技术 实验九 Web安全基础

    20155201 网络攻防技术 实验九 Web安全基础 一.实践内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.报告内容: 1. 基础问题回答 1)SQL注入攻击 ...

  10. 定制FileField中的上传文件名称

    FileField中的upload_to属性可以设定上传文件的存储目录和名称,它可以是个字符串,也可以是个callable,比如一个方法. 当upload_to的值设为一个方法时,就可以对上传文件的名 ...