Table Attribute in Entity Framework Core

Table Attribute in Entity Framework Core

In this article, I am going to discuss Table Data Annotation Attribute in Entity Framework Core (EF Core) with Examples. 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.

All the Data Annotation Attributes are classes that 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 two properties, one constructor and one private field. The constructor takes one string parameter, which is nothing but the database table name, and this 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 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 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 is going to 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 model using add-migration and update-database commands using Package Manager Console or .NET Core CLI.

So, open Package Manager Console and Execute the following add-migration and update-database commands. 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, then 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 Package Manager Console and Execute the following add-migration and update-database commands. So, every time we make changes to 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 shown in the image below.

Table Data Annotation Attribute in EF Core

In the next article, I am going to 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 *