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

Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

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

标签:

DataAnnotations - Table Attribute:

Table attribute can be applied to a class. Default Code-First convention creates a table name same as the class name. Table attribute overrides this default convention. EF Code-First will create a table with a specified name in Table attribute for a given domain class.

Consider the following example.

using System.ComponentModel.DataAnnotations.Schema;

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

 

As you can see in the above example, Table attribute is applied to Student class. So, Code First will override default conventions and create StudentMaster table instead of Student table as shown below.

技术分享

You can also specify a schema for the table using Table attribute as shown below.

using System.ComponentModel.DataAnnotations.Schema;

[Table("StudentMaster", Schema="Admin")]
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    public string StudentName { get; set; }
        
}

 

Code-First will create StudentMaster table in Admin schema as shown below.

技术分享

Entity Framework Code-First(9.7):DataAnnotations - Table Attribute

标签:

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

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