Part 4 Customizing table, column and foreign key column names when using entity framework code fir04:33

  • 0
Published on April 22, 2017

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

In this vide we will discuss, customizing table, column and foreign key column names when using entity framework code first approach. This is continuation to Part 3. Please watch Part 3 before proceeding.

In Part 3, we have discussed generating Departments and Employees tables using Entity Framework Code first approach.

Entity Framework generated the Employees table. Notice the column names. Department_Id column has an underscore in it’s name. Let’s say we want the column to be generated as DepartmenId (without an underscore)

Entity Framework generated the above Employees table based on the following custom Employee class that we created.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public Department Department { get; set; }
}

To achieve this use the ForeignKey attribute present in System.ComponentModel.DataAnnotations.Schema namespace. Modify the Employee class as shown below.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
[ForeignKey(“DepartmentId”)]
public Department Department { get; set; }
}

Rebuild the solution, and run the application.

You may get the following error. We will discuss the reasons for this error and how to fix it the right way in a later video session.
The model backing the ‘EmployeeDBContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (

For now to get around the error, delete the Sample database using SQL Server Management Studio, and then try to run the application again. A blank webform will be displayed. Now check the Employees tables using SQL Server Management Studio and notice that the DepartmentId column is created without an underscore as expected.

To customize the table name, use Table attribute and to customize column name use Column attribute.

For example, to change
Table name from Employees to tblEmployees and
FirstName column to First_Name

We would modify the Employee class as shown below.
[Table(“tblEmployees”)]
public class Employee
{
public int Id { get; set; }
[Column(“First_Name”)]
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
[ForeignKey(“DepartmentId”)]
public Department Department { get; set; }
}

Enjoyed this video?
"No Thanks. Please Close This Box!"