DataAnnotations - StringLength Attribute:

StringLength attribute can be applied to a string type property of a class. EF Code-First will set the size of a column as specified in StringLength attribute. Note that it can also be used with ASP.Net MVC as a validation attribute.

Consider the following example.

using System.ComponentModel.DataAnnotations;

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

As you can see in the above code, we have applied StringLength attribute to StudentName. So, Code-First will create a nvarchar(50) column StudentName in the Student table as shown below.

Entity Framework also validates the value of a property for StringLength attribute if you set the value more than the specified size. For example, if you set more than 50 chars long StudentName then EF will throw EntityValidationError.

Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute的更多相关文章

  1. Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute

    DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...

  2. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...

  3. Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute

    DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...

  4. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  5. Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

    DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...

  6. Entity Framework Code-First(9.2):DataAnnotations - TimeStamp Attribute

    DataAnnotations - TimeStamp Attribute: TimeStamp attribute can be applied to only one byte array pro ...

  7. Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

    DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...

  8. Entity Framework Code-First(9.4):DataAnnotations - Required Attribute

    Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...

  9. Entity Framework Code-First(9.3):DataAnnotations - ConcurrencyCheck Attribute

    ConcurrencyCheck Attribute: ConcurrencyCheck attribute can be applied to a property of a domain clas ...

随机推荐

  1. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  2. es6对象内函数的两种写法

    es6对象内函数一般有两种写法: var person1 = { name: "p1", sayThis() { console.log(this); } }; var perso ...

  3. linux下安装telnet(centos7)

    1 环境 centos 7 yum已配置好 网络连接正常 2 检查是否已安装相关软件 rpm -qa|grep telnet 3 安装相关组件 yum install xinetd yum insta ...

  4. TYOI Day1 travel:Tree dp【处理重复走边】

    题意: 给你一棵树,n个节点,每条边有长度. 然后有q组询问(u,k),每次问你:从节点u出发,走到某个节点的距离mod k的最大值. 题解: 对于无根树上的dp,一般都是先转成以1为根的有根树,然后 ...

  5. Array对象(一)

    Array是JavaScript中的一个事先定义好的对象(也可以称作一个类),可以直接使用. 创建Array对象: var array = new Array(); 创建指定元素个数的Array对象: ...

  6. centos虚拟机启用网卡

    CentOS虚拟机安装成功后,默认开机未启用网关,通过修改配置文件,启用网卡 编辑系统配置文件,虚拟机完成后,系统安装了一个默认的网卡,即eth0,其配置文件的路径为/etc/sysconfig/ne ...

  7. python 面试题(一)

    1 Python的函数参数传递 看两个例子: Python   1 2 3 4 5 a = 1 def fun(a):     a = 2 fun(a) print a  # 1   Python   ...

  8. Linux tar.gz 、zip、rar 解压 压缩命令

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  9. appium-环境搭建(二)

    接着上一篇 1.基本的python环境 2.安装node.js,32位还是64位根据自己的系统来选择 3.安装.net framwork4.5 4.安装appium 安装完成后,运行appium.ex ...

  10. tomcat报错:Wrapper cannot find servlet class ...

    tomcat发布工程时,在浏览器输入正确的地址,遇到如下问题: HTTP Status 500 - javax.servlet.ServletException: Wrapper cannot fin ...