原文链接: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. java编程思想(1)--对象导论

    对象导论: 1.1 抽象过程 所有的语言都有抽象机制,抽象是解决复杂问题的根本方法.例如:汇编语言是对底层机器的轻微抽象.命令式语言(如:FORTRAN.BASIC.C)又是对汇编语言的抽象. jav ...

  2. <记录> curl 封装函数

    1. POST请求 参数1 : 请求地址 参数2 : 数组形式的参数 /** * @param string $url post请求地址 * @param array $params * @retur ...

  3. c#读取文本并生成txt

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 使用List需要注意的点

    目录 1. 概述 2. Arrays.asList(); 2-1. 产生不可操作的集合 2-2. 需要传入对象数组而不是基本类型数组 3. arrayList.subList(); 3-1. subL ...

  5. 反射与特性与Tool编写

    大多数程序都是用来处理数据的,他们读,写,操作和显示数据,图形也是一种数据. 程序员为某种目的创建和使用一些类型,因此,在设计时必须理解所使用类型的特性. 有关程序及其类型的数据被称为元数据,他们保存 ...

  6. 记账本,C,Github,结果

  7. Unity之AssetBundle打包

    AssetBundle Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resource ...

  8. js正则积累

    判断是否为数字 function isNumber(val){ var regPos = /^\d+(\.\d+)?$/; //非负浮点数 var regNeg = /^(-(([0-9]+\.[0- ...

  9. wampserver安装

    WampServer的下载地址:https://sourceforge.net/projects/wampserver 安装教程:https://jingyan.baidu.com/article/4 ...

  10. 给对话框添加动画 Dialog

    先添加一个动画文件(res->anim文件夹中),文件名为a.xml <?xml version="1.0" encoding="utf-8"?&g ...