码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-05 17:08:08      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

DataAnnotations - Column Attribute:

Column attribute can be applied to properties of a class. Default Code First convention creates a column name same as the property name. Column attribute overrides this default convention. EF Code-First will create a column with a specified name in Column attribute for a given property.

Consider the following example.

using System.ComponentModel.DataAnnotations.Schema;

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

 

As you can see in the above example, Column attribute is applied to StudentName property of Student class. So, Code-First will override default conventions and create Name column instead of StudentName column in the Student table, as shown below.

技术分享

You can also specify an order and type of the column using Column attribute, as shown below.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [Column("Name", Order=1, TypeName="varchar")]
    public string StudentName { get; set; }
        
}

 

The above code creates Name column of varchar type as a first column in Student, as shown below.

技术分享

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

标签:

原文地址:http://www.cnblogs.com/purplefox2008/p/5644198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!