【译】第15节---数据注解-StringLength
StringLength属性可以应用于类的字符串类型属性。 EF Code First将设置StringLength属性中指定的列的大小。 请注意,它也可以与ASP.Net MVC一起用作验证属性。
请看以下示例:
using System.ComponentModel.DataAnnotations; public class Student
{
public Student()
{ }
public int StudentID { get; set; } [StringLength()]
public string StudentName { get; set; } }
如上面的代码所示,我们已经将StringLength属性应用于StudentName。 所以,Code-First将在Student表中创建一个nvarchar(50)列StudentName,如下所示:

如果设置的值大于指定的大小,则EF还会验证StringLength属性的属性值。
例如,如果您设置了超过50个字符长StudentName,则EF将抛出EntityValidationError。
关于StringLength和MaxLength的区别,请移步:https://stackoverflow.com/questions/5717033/stringlength-vs-maxlength-attributes-asp-net-mvc-with-entity-framework-ef-code-f
【译】第15节---数据注解-StringLength的更多相关文章
- 【译】第20节---数据注解-InverseProperty
原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...
- 【译】第19节---数据注解-NotMapped
原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...
- 【译】第18节---数据注解-ForeignKey
原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...
- 【译】第17节---数据注解-Column
原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...
- 【译】第16节---数据注解-Table
原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.a ...
- 【译】第14节---数据注解-MaxLength/MinLength
原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...
- 【译】第13节---数据注解-Required
原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...
- 【译】第12节---数据注解-ConcurrencyCheck
原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...
- 【译】第11节---数据注解-TimeStamp
原文:http://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code-fir ...
随机推荐
- formdata 和 Payload 区别
FormData和Payload是浏览器传输给接口的两种格式,这两种方式浏览器是通过Content-Type来进行区分的(了解Content-Type),如果是 application/x-www-f ...
- 转:异常处理之ThreadException、unhandledException及多线程异常处理
转载自:http://www.cnblogs.com/levin9/articles/2319251.html 一:ThreadException和unhandledException的区别 处理未捕 ...
- idea下导入Tomcat源码
对于web开发者来说,如果明白了tomcat那对于开发还是后面的学习都是有很大益处的,但在网上看了很多的文章,总是没弄好,经历了很久才弄好了,写个文章记录下,希望也能帮助到其他人.下载Tomcat源码 ...
- HashMap和LinkedHashMap区别
import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.uti ...
- Java基础再复习(继承、多态、方法内部类**、HashMap用法**、参数传递**)
###继承: package com.shiyan; public class Animal { public int legNum; //动物四肢的数量 //类方法 public void bark ...
- 利用crontab定时备份nginx访问日志(也可以说是定时切分日志)
在我们的工作中,肯定会涉及到分析访问日志. 但是如果访问日志都集中存在于一个文件中,那数据量就太大了,并且也不利于我们进行分析. 所以我们需要对访问日志进行按时间切割. 思路: 我们可以利用linux ...
- MySQL5.7 忘记root密码,怎么破?
MySQL5.7 忘记root密码,怎么破? 关服 # kill $mysql_pid 免密启动 # /usr/local/mysql57/bin/mysqld_safe --defaults-fil ...
- Introduction to the Standard Directory Layout
Having a common directory layout would allow for users familiar with one Maven project to immediatel ...
- setfacl 设置文件访问控制列表
setfacl 设置文件访问控制列表 用法: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ... 参数: -m, --modify=acl 更改文件的访问控 ...
- gdb调试问题汇总
1. 宏调试 在GDB下,我们无法print宏定义,因为宏是预编译的.但是我们还是有办法来调试宏,这个需要GCC的配合. 在GCC编译程序的时候,加上-ggdb3参数,这样,你就可以调试宏了.另外,你 ...