Data Annotations - Table Attribute in EF 6 & EF Core

The Table attribute can be applied to a class to configure the corresponding table name in the database. It overrides the default convention in EF 6 and EF Core. As per the default conventions, EF 6 creates a table name matching with <DbSet<TEntity> property name> + 's' (or 'es') in a context class and EF Core creates the Db column with the same name as DbSet<TEntity> property name.

Table Attribute: [Table(string name, Properties:[Schema = string])

  • name: Name of the Db table.
  • Schema: Name of the Db Schema in which a specified table should be created. (Optional)
using System.ComponentModel.DataAnnotations.Schema;

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

In the above example, the Table attribute is applied to the Student entity class. So, EF will override the default conventions and create the StudentMaster table instead of the Students table in the database, as shown below.

dataannotations table attribute

Use the Schema property to specify the schema name for a Db table as shown below.

using System.ComponentModel.DataAnnotations.Schema;

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

EF will create the StudentMaster table in the Admin schema as shown below.

dataannotations table attribute

Test Your Entity Framework Knowledge: