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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Entity Framework Code-First(9.7):DataAnnotations - Table Attribute相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Entity Framework Code-First(23):Entity Framework Power Tools

Entity Framework怎么GroupBY多个字段

entity framework 公共类

Entity Framework Fluent API

初步了解Entity Framework

Entity Framework 中的in操作实例