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

EF 6 Code-First系列文章目录:

MaxLength特性指定了属性的值所允许的最大值,然后在数据库中就生成相应列的最大值。MaxLength特性可以应用于实体的String类型的属性和byte[]数组类型的属性上。

如果MaxLength特性,应用在其他类型的属性上就报错,例如下面的图中例子:

using System.ComponentModel.DataAnnotations;

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

上面代码例子中,MaxLength(50)应用在StudentName属性上,指定StudentName的属性值不能超过50个字符长度。

EF将会检查标识了MaxLength特性的属性值,如果长度超过了指定的长度,就会报错,EF6报错:System.Data.Entity.Validation.DbEntityValidationException ,EF Core报错:Microsoft.EntityFrameworkCore.DbUpdateException.

请注意:MaxLength特性,同样可以用在ASP.NET MVC中,用于验证属性的值,了解更多详情请看这篇文章: Implement Validations in ASP.NET MVC

9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】的更多相关文章

  1. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  2. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  3. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

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

  4. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

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

  5. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  6. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

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

  7. 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code ...

  8. 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...

  9. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

随机推荐

  1. alias命令详解:给命令设置别名

    给命令设置别名,你可以把它当作命令的"小名",但是这样做有什么意义呢? 比如笔者刚接触 Linux 时,使用的编辑器是 Vi,但是现在 Vim 的功能明显比 Vi 的功能更加强大, ...

  2. js day01

    一)什么是JavaScript[以下简称JS] JS是 (1)基于对象 JS本身就有一些现成的对象可供程序员使用,例如:Array,Math,String... JS并不排除你可以自已按一定的规则创建 ...

  3. Linux中近期使用高频命令小结

    配置定时任务命令crontab : 用来增加系统的定时任务,可以指定用户,时间,以及相关指令: 查看端口是否相通,扫描端口nc: nc -v ip地址 端口号: 转换格式命令dos2unix: 用来将 ...

  4. java DES加解密及Wrong key size错误

    如下的DES加密方法会报错:Wrong key size public static String encryptDES(String source) throws Exception{ Secret ...

  5. 云笔记项目-笔记列表弹出"分享移动删除"子菜单

    业务需求: 笔记列表里还有一个按钮可以弹出子菜单,要求做到以下几点: (1)点击选中的笔记行的弹出按钮后,弹出子菜单,再次点击,子菜单收回. (2)选中其他笔记后,子菜单消失.效果如下图所示: 业务分 ...

  6. Jenkin配置执行远程shell命令

    转载自 http://www.cnblogs.com/parryyang/p/6261730.html 在利用jenkins进行集成化部署的时候,我们在部署生成的war包时,往往需要进行一些备份,或者 ...

  7. NodeJs学习相关网址

    node官方中文 https://nodejs.org/zh-cn/   Node.js 中文网 https://nodejs.org/zh-cn/   Node.js 教程 | 菜鸟教程 http: ...

  8. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  9. 团队项目NABCD分析

    1.卡片分类讨论 我们团队在软件工程课上对准备开发的帮你项目进行激烈的讨论后,得出了两个特点. (1)信息检索 (2)主动推送 之所以得出这两个特点,是因为我们作为学生,平常在校园里面有很多专用群和Q ...

  10. swift 需求: 导航栏和HeaderView 使用一个背景图片。

    问题界面 需求: 导航栏和HeaderView 使用一个背景图片.解决方案: 让 导航栏 变成透明. override func viewWillAppear(_ animated: Bool) { ...