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. EntityFramework 学习 一 CRUD using Stored Procedure: 使用存储过程进行CRUD操作

    我们先创建如下3个存储过程 1.Sp_InsertStudentInfo: CREATE PROCEDURE [dbo].[sp_InsertStudentInfo] -- Add the param ...

  2. 大话设计模式--解释器模式 interpreter -- C++实现实例

    1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...

  3. 大话设计模式--代理模式 proxy

    1. 代理模式: 为其他对象提供一种代理以控制这个对象的访问. 代理模式使用场合: a. 远程代理, 为一个对象在不同的地址空间提供局部代理,隐藏一个对象存在于不同地址空间的事实.如.net中WebS ...

  4. 解决xhost: unable to open display ""

    首先安装vncserver,如图: 切换账户:sudo su到root下 执行:export DISPLAY=:0.0 执行:xhost +,如图:

  5. PLSQL Developer使用技巧整理

      Shortcut: =============================================================================== Edit/Und ...

  6. PHP+MySQL百万级数据插入的优化

    插入分析 MySQL中插入一个记录需要的时间由下列因素组成,其中的数字表示大约比例: 连接:(3) 发送查询给服务器:(2) 分析查询:(2) 插入记录:(1x记录大小) 插入索引:(1x索引) 关闭 ...

  7. pom详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. STL memory.cpp

    memory.cpp # // Filename: memory # # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: ht ...

  9. Spring MVC 学习第一篇

    很好的MVC 参考blog:http://jinnianshilongnian.iteye.com/blog/1752171 MVC: 概念:是一种设计模式,并没有引入新的技术,只是把我们开发的结构组 ...

  10. ngget配置

    Install-Package NuGet.CommandLine nuget spec nuget pack Jryg.VirtualNumber.ClientNet4.csproj -Includ ...