原文: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基础命令---删除用户userdel

    userdel 删除用户,如果没有附加选项,仅删除用户,不删除相关文件. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法   ...

  2. navicat链接阿里云mysql报80070007: SSH Tunnel: Server does not support diffie-hellman-group1-sha1 for keyexchange

      http://www.jianshu.com/p/200572ed066c navicat 链接数据库 使用navicat 的ssh通道连接数据库回遇到权限问题 错误代码如下: 80070007: ...

  3. javanio2

    package com.lanhuigu.nio.selector; import java.net.InetSocketAddress; import java.nio.ByteBuffer; im ...

  4. 1分钟完美安装最新CentOS+Nginx+PHP-FPM+MySQL

    PHP 5.3.1 MySQL 5.0.89 Nginx 0.8.33 或 0.7.65 (可选) 现在,我们可以快速全自动搞定 CentOS + Nginx + PHP-FPM + MySQL 的安 ...

  5. 垂直打击之JVM剖析

    让Java应用程序运行是一回事,但让他们跑得快就是另外一回事了.在面对对象的环境中,性能问题就像来势凶猛的野兽.但JVM的复杂性将性能调整的复杂程度增加了一个级别.这里Refcard涵盖了JVM in ...

  6. 运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration!

    运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration! 问题: 运行android模 ...

  7. 上传代码到github的步骤

    在你的电脑上装好git 大致流程是: 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.g ...

  8. VMware Ubuntu 虚拟机安装 VMwareTools (VMware虚拟机如何与主机互相复制文件)

    1.关闭虚拟机 2.CD-ROM开机连接取消对号 3.开启虚拟机 4.此时可能提示安装,点击即可 或者在VMware上方选择 :虚拟机 → 安装VMware Tools 5.虚拟机桌面会弹出相应安装包 ...

  9. 前端 --- 3 css 属性

    一. 标签嵌套规则 块级标签能够嵌套某些块级标签和内敛标签(行内标签) 内敛标签不能嵌套块级标签,只能嵌套内敛标签 二.   属性 1.宽和高 (块级标签能够设置高度和宽度 内敛标签不能设置,设置了没 ...

  10. python --- 19 判断对象所属,区分函数和对象, 反射

    一.判断对象所属 isinstance, type , issubclass 1.issubclass(x,y)    判断x是否是y 的子类 2.type(x)  精准返回x 的数据类型 3.isi ...