Back to: Entity Framework Tutorials For Begineers and Professionals
Table Data Annotation Attribute in Entity Framework
In this article, I am going to discuss Table Data Annotation Attribute in Entity Framework Code First Approach with Examples. Please read our previous article where we discussed How to Configure Domain Classes using Data Annotation Attributes in Entity Framework Code-First Approach with Examples.
Table Data Annotation Attribute in Entity Framework Code First:
The Table Data Annotation Attribute in Entity Framework Code First Approach can be applied to a domain class to configure the corresponding database table name and schema. It overrides the default convention in Entity Framework. As per the default conventions, Entity Framework API will create a database table whose name is matching with <Entity Name> + ‘s’ (or ‘es’) in a context class.
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.
As you can see in the above TableAttribute class, it is having two properties and one constructor. The constructor is taking 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:
- Name: Gets the name of the table the class is mapped to. It is a read-only property.
- Schema: Gets or sets 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 in .NET.
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 Attribute in Entity Framework:
Let us understand Table Data Annotation Attribute in Entity Framework Code First Approach 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 run the application, it should create a database table with the name StudentInfo which is going to map with the following Student Entity. The Table Attribute belongs to System.ComponentModel.DataAnnotations.Schema namespace.
using System.ComponentModel.DataAnnotations.Schema; namespace EFCodeFirstDemo { [Table("StudentInfo")] public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } }
In the above example, the Table Attribute is applied to the Student Entity class. So, the Entity Framework 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.
As we are going to update the model many times, in order to avoid the Run-Time Exception when the model changes, let us set the database initializer as DropCreateDatabaseIfModelChanges. So, modify the context class as follows:
using System.Data.Entity; namespace EFCodeFirstDemo { public class EFCodeFirstContext : DbContext { public EFCodeFirstContext() : base("name=MyConnectionString") { //Setting the Database Initializer as DropCreateDatabaseIfModelChanges Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFCodeFirstContext>()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } public DbSet<Student> Students { get; set; } } }
Please make sure to have the connection string with the name MyConnectionString within the app.config file or web.config file as shown in the below image.
Now, modify the Main method of the Program class as follows. There are no differences in adding an entity into the database between the code first and the database first approach in the entity framework. In fact, the way we are performing the database CRUD operations using Entity Framework Database First Approach, in the same way, we are going to perform the CRUD operations in Entity Framework Code First Approach.
using System; using System.ComponentModel.DataAnnotations.Schema; namespace EFCodeFirstDemo { class Program { static void Main(string[] args) { using (EFCodeFirstContext context = new EFCodeFirstContext()) { var student = new Student() { FirstName = "Pranaya", LastName="Rout" }; context.Students.Add(student); context.SaveChanges(); Console.WriteLine("Student Added"); //Fetch Table Attribute Details GetAttribute(typeof(Student)); Console.ReadKey(); } } public static void GetAttribute(Type t) { // Get instance of the attribute. TableAttribute tableAttribute = (TableAttribute)Attribute.GetCustomAttribute(t, typeof(TableAttribute)); if (tableAttribute == null) { Console.WriteLine("The Attribute was not found."); } else { // Get the Name value. Console.WriteLine($"Name of Table is: {tableAttribute.Name}"); // Get the Schema value. Console.WriteLine($"Schema of Table is: {tableAttribute.Schema}"); } } } }
Now, run the application and you should get the following output. As you can see, it is printing the table name as StudentInfo, and as we have not set the schema so it is printing null. But in the database, it will create the table with the default schema i.e. dbo.
You can also verify the database and you should see the following.
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 EFCodeFirstDemo { [Table("StudentInfo",Schema ="Admin")] public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } }
Now, with the above changes in place, run the application and you will get the following output. As you can see, it is printing the table name as StudentInfo and the schema name as Admin as expected.
You can also verify the database and you should see the schema as Admin and the table name as StudentInfo shown in the below image.
In the next article, I am going to discuss Column Data Annotation Attribute in Entity Framework Code First Approach with Examples. Here, in this article, I try to explain Table Data Annotation Attribute in Entity Framework Code First Approach with Examples. I hope you enjoyed this Table Data Annotation Attribute in Entity Framework Code First Approach with Examples article. Please give your valuable feedback and suggestions about this article.