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. 【转载】xtrabackup原理及实施

    转载于:http://www.baidu-ops.com/2013/05/26/xtrabackup/ xtrabackup是基于InnoDB存储引擎灾难恢复的.它复制InnoDB的数据文件,尽管数据 ...

  2. 【Flask】Column常用参数

    ### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字 ...

  3. X-real-ip与X-Forwarded-For

    经过反向代理后,客户端与web服务器之间添加了中间层,因此: 1.代理服务器使用$remote_addr拿到的会是客户端的ip 2. web服务器使用$remote_addr拿到的会是代理服务器的ip ...

  4. shell脚本默认变量值

    脚本参数相关: $# 是传给脚本的参数个数 $ 是脚本本身的名字 $ 是传递给该shell脚本的第一个参数 $ 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单 ...

  5. XP最高支持IE8

    1.https://www.microsoft.com/zh-CN/download/details.aspx?id=24488 适用于 Windows XP 的 Internet Explorer ...

  6. linux 新建分区 、格式化 并挂载的命令

    一.新建分区命令为 fdisk /dev/diskname fdisk命令为交互式命令 p:显示当前硬盘上的分区,包括没保存的改动 n:创建新分区 e:表示扩扩展分区 p:表示主分区 d:删除一个分区 ...

  7. POJ 2253 Frogger(warshall算法)

    题意:湖中有很多石头,两只青蛙分别位于两块石头上.其中一只青蛙要经过一系列的跳跃,先跳到其他石头上,最后跳到另一只青蛙那里.目的是求出所有路径中最大变长的最小值(就是在到达目的地的路径中,找出青蛙需要 ...

  8. Netty组件理解(转载)

    转载的文章,写的非常好.   一.先纵览一下Netty,看看Netty都有哪些组件?        为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架 ...

  9. ffmpeg用法(心得体会还有你见过的用法)

    ffmpeg的常用用法很多,我这里提供的用法有可能有许多地方是你没见过的. 一.ffmpeg合并视频 我经常需要切割再把一些零碎的视频给拼接起来,这样可以省许多磁盘空间.其实用mencoder挺不错的 ...

  10. log4net初探

    /// <summary> /// Static constructor that initializes logging by reading /// settings from the ...