Table Attribute in Entity Framework Core

Table Attribute in Entity Framework Core

In this article, I will discuss Table Data Annotation Attribute in Entity Framework Core (EF Core) with Examples. Please read our previous article discussing the basics of Data Annotation Attributes in Entity Framework Core. The Table Data Annotation Attribute is applied to an entity to specify the name of the database table that the entity should map to.

Table Data Annotation Attribute in Entity Framework Core

The Table Data Annotation Attribute in Entity Framework Core can be applied to a domain class to configure the corresponding database table name and schema. It overrides the default convention in Entity Framework Core. As per the default conventions, Entity Framework Core will create a database table whose name is the same as the DbSet Property name.

Note: In Entity Framework Core (EF Core), the [Table] Data Annotation Attribute is used to configure the database table name to which an entity should be mapped. This is particularly useful if the default table name EF Core generates based on the entity class name doesn’t meet your requirements or if you’re working on an existing database and must match table names exactly.

All the Data Annotation Attributes are inherited from the Attribute abstract class. Now, if you go to the definition of Table Attribute class, then you will see the following.

Table Data Annotation Attribute in Entity Framework Core

As you can see in the above TableAttribute class, it has one constructor, two properties, and one private field. The constructor takes one string parameter, which is nothing but the database table name, which is mandatory. The Schema property is optional, and the use of the Name and Schema properties are as follows:

  1. Name: The name of the table the class is mapped to. It is a read-only property.
  2. Schema: The schema of the table the class is mapped to. This is optional. It is a read-write property.

Note: Using square bracket [], we need to specify the attributes.
Syntax to use Table Attribute: [Table(string name, Properties:[Schema = string])
Example to use Table Attribute: [Table(“StudentInfo”, Schema=”Admin”)]

Examples to understand Table Data Annotation Attributes in EF Core:

Let us understand the Table Data Annotation Attribute in Entity Framework Core with an example. Let us modify the Student Entity class as follows. As you can see, we have specified the table name as StudentInfo. So, when we add the migration and update the database, it should create a database table with the name StudentInfo in the database, which will map with the following Student Entity. The Table Attribute belongs to the System.ComponentModel.DataAnnotations.Schema namespace.

using System.ComponentModel.DataAnnotations.Schema;
namespace EFCoreCodeFirstDemo.Entities
{
    [Table("StudentInfo")]
    public class Student
    {
        public int StudentId { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }
}

The Table Attribute is applied to the Student Entity class in the above example. So, the Entity Framework Core will override the default conventions and create the StudentInfo database table instead of the Students table in the database, which will be mapped with the above Student Entity class. Next, modify the context class as follows. As you can see, we only include the Student as a DbSet Property.

using Microsoft.EntityFrameworkCore;
namespace EFCoreCodeFirstDemo.Entities
{
    public class EFCoreDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=LAPTOP-6P5NK25R\SQLSERVER2022DEV;Database=EFCoreDB;Trusted_Connection=True;TrustServerCertificate=True;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }

        public DbSet<Student> Students { get; set; }
    }
}

Note: As we already discussed, whenever we add or update domain classes or configurations, we need to sync the database with the codebase using add-migration and update-database commands using Package Manager Console or .NET Core CLI.

So, open the Package Manager Console and Execute the add-migration and update-database commands as follows. You can give any name to your migration. Here, I am giving Mig1. The name that you are giving it should not be given earlier.

Table Data Annotation Attribute in Entity Framework Core (EF Core) with Examples

Now, you can also verify the database, and you should see the following.

Table Data Annotation Attribute in Entity Framework Core (EF Core) with Examples

As you can see, it created the database table name that we specified in our Student model class using the Table Attribute. Further, you can notice it is created with the schema dbo. Now, if you want to create the table with a different schema, such as Admin, you need to use the Schema property of the Table Attribute. For a better understanding, please modify the Student entity as follows.

using System.ComponentModel.DataAnnotations.Schema;
namespace EFCoreCodeFirstDemo.Entities
{
    [Table("StudentInfo", Schema = "Admin")]
    public class Student
    {
        public int StudentId { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
    }
}

So, again, open the Package Manager Console and Execute the add-migration and update-database commands as follows. So, every time we change our model, we need to execute the following two commands. I cannot use Mig1, so I am giving the migration name Mig2.

Table Data Annotation Attribute in Entity Framework Core

Now, you can verify the database and see the schema as Admin and the table name as StudentInfo, as shown in the image below.

Table Data Annotation Attribute in EF Core

When to use Table Attribute in EF Core?
  • When you are working with an existing database, and need to match table names exactly.
  • When you want to follow a naming convention in your database that’s different from your entity class naming convention.
  • When you need to specify a schema other than the default.

By using the [Table] attribute, we can assign the table name and schema in a declarative way, which makes the intent within the entity class more clear. However, the Fluent API can also achieve the same configuration and is more powerful, offering additional configuration capabilities that data annotations cannot provide.

In the next article, I will discuss Column Data Annotation Attribute in Entity Framework Core with Examples. In this article, I try to explain Table Data Annotation Attribute in Entity Framework Core with Examples. I hope you enjoyed this Table Attribute in EF Core with Examples article. Please give your valuable feedback and suggestions about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *