【译】第16节---数据注解-Table

原文:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.aspx

Table 属性可以应用于一个类。 默认Code First约定创建与名称相同的表名称。Table属性覆盖此默认约定。 EF Code First将在给定域类的Table属性中创建一个具有指定名称的表。

请看以下示例:

using System.ComponentModel.DataAnnotations.Schema;

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

如上例所示,Table属性应用于Student类。 因此,Code First将覆盖默认约定,并创建StudentMaster表而不是Student表,如下所示:

你还可以使用Table属性指定表的模式(schema ),如下所示:

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将在Admin模式中创建StudentMaster表,如下所示:

原文地址:https://www.cnblogs.com/talentzemin/p/7218170.html