原文:http://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-code-first.aspx

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的更多相关文章

  1. 【译】第20节---数据注解-InverseProperty

    原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...

  2. 【译】第19节---数据注解-NotMapped

    原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-fir ...

  3. 【译】第18节---数据注解-ForeignKey

    原文:http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-fi ...

  4. 【译】第17节---数据注解-Column

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

  5. 【译】第16节---数据注解-Table

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

  6. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  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. python XML文件解析:用xml.dom.minidom来解析xml文件

    python解析XML常见的有三种方法: 一是xml.dom.*模块,是W3C DOM API的实现,若需要处理DOM API则该模块很合适, 二是xml.sax.*模块,它是SAX API的实现,这 ...

  2. CSS布局实战

    1. ul li中上图下字,图片.文字居中. <ul> <li> <img src="dash.png" style="display:bl ...

  3. tft屏图像显示也成功完成

    2010-04-30 14:18:00 tft屏图像显示也成功完成. 其实有了刷屏的经验,图像显示就很简单. void address_set(uint x1,uint y1,uint x2,uint ...

  4. vue路由5:命名视图

    <div id="app"> <div> <router-link to="/">首页</router-link> ...

  5. spark机器学习笔记01

     1)外部数据源 val distFile1 = sc.textFile("data.txt") //本地当前目录下文件 val distFile2 =sc.textFile(& ...

  6. Django框架----外键关联

    app/models.py中: 创建班级表 class classes(models.Model): id = models.AutoField(primary_key=True) name = mo ...

  7. 关于springMVC 传递 对象参数的问题

    1.前端请求必须是 post 2.前端数据data必须做 json字符串处理  JSON.stringify(data) 3. contentType: 'application/json', 4.@ ...

  8. jt项目菜单页面实现

    jt项目菜单页面实现 一. 业务描述: 1) 数据呈现时使用bootstrap中的treeGrid插件(基于jquery实现). bootstrap特点:简洁.直观.强悍.移动设备优先的前端开发框架, ...

  9. 2018-2019-1 20189206 《Linux内核原理与分析》第七周作业

    linux内核分析学习笔记 --第六章 进程的描述和进程的创建 学习重点--子进程的创建以及运行流程 进程描述和进程的创建 操作系统的三大功能--进程管理.内存管理和文件系统. 在linux内核中利用 ...

  10. Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序

    In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...