filmov
tv
Part 4 Customizing table, column and foreign key column names when using entity framework code fir

Показать описание
Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Entity Framework - All Text Articles
Entity Framework - All Slides
Entity Framework Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
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.
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; }
}
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
Slides
Entity Framework - All Text Articles
Entity Framework - All Slides
Entity Framework Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
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.
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; }
}
Комментарии