文章出處

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(50)]
    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.

dataannotations maxlength attribute

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.


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()