您的位置:首页 > 产品设计 > UI/UE

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

2016-07-05 16:30 477 查看
Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT NULL column in a database table for a property on which we apply Required 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; }

[Required]
public string StudentName { get; set; }

}


As you can see in the above code, we have applied Required attribute to StudentName. So, Code First will create a NOT NULL StudentName column in the Student table as shown below.



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: