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

preview_player
Показать описание
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; }
}
Рекомендации по теме
Комментарии
Автор

You helped me to create my first CRUD application that is ACTUALLY WORKING. I hope you are honored with a microsoft associate title.

Babaelow
Автор

All I can say Sir is "GOD BLESS YOU"

jaironaranjo
Автор

THANK YOU! OMG!!!

I have always used your  videos for learning MVC 5 + EF 6.1, and this was haunting me!!! I didn't know you could always specify a foreign key with a Data Annotation. I've been lingering for this solution for Yes yes yes yes yes yes yes yes yes yes yes yes yes yes! :D :D :D.

I'm too excited! Woohohoh. I was doing some barbarities in the code, until I found this! Thank you so

superjosereborn
Автор

All videos are very much clear and helpful. Thanks a lot.

pravallikaannangi
Автор

there is no better teacher than you that I know of. Please tell us if you have written any book about programming? I think many of us would like to get such a book?

alpsavasdev
Автор

Hi, First of all - great work! These are very thorough tutorials & I have learned a lot from them.

I'd like to ask that you also add a tutorial that addresses the Database First paradigm, as I'm currently trying to learn to use Entity Framework form this perspective. That is, we have an existing database that has other software making use of it. Therefore, using Entity Framework to rename tables and/or fields in the database is going to break those other applications. Nevertheless, it would be good to be able to alias names for readability sake, etc.

Thanks for this fine series. :-)

evanflink
Автор

Really nice sir . Thank you for awsome e-teaching.

girijeshyadav
Автор

Nice tutorial... useful to beginners...

karunakaranr
Автор

Hello Venka, I am interested to see how to implement update, insert, delete methods in the Employee Repository based on what has been done so far? Thanks a lot
Zoe

dongfanzhang
Автор

Hi Venkat thanks for this. I have question. How do I provide user chosen primary key. Not the database generated. Tried multiple things but nothing worked.

HarshRaj-pugi
Автор

Yes but, each time we add or rename a table we must drop the database?
Is there any other solution to this?
Thank you.
George

georgekarakasidis
Автор

can we use code first approach for existing database?

gkmishra
Автор

Sir i am using the Identity column in my SQL server database table so, and i am using the Entity framework 6.1.0, then how can i handle that Autogenrated column in my c# code while inserting the data.

akhileshsahu
Автор

CHANGE THE EXISTING DATA STRUCTURES TO ADD DEPARTMENT INFORMATION OF AN EMPLOYEE .
AT ANY POINT OF TIME AN EMPLOYEE CAN BE ALLOCATED TO MULTIPLE DEPARTMENTS WITH AN ALLOCATION PERCENTAGE,
BUT SUM OF ALLOCATION CANOT EXCEED 100%.
PROVISION SHOULD BE PRESENT TO RETRIEVE EMPLOYEE DEPARTMENT ALLOCATION FOR ANY PREVIOUS DATE.

please give the answer or any hint to write a query....

foxfiredancestudio
Автор

Sir, while creating foreign key reference.. Am getting error as cannot insert null values.. Update fails.. Pls help

yhu
Автор

how to retrive foreign key data using entity freamwork in mvc razor view???

archinmodi
Автор

give video how we can call web api in wcf

gkmishra
Автор

Absolutely useless unless you already know how to program to the same standard as the presenter. Basic explanations not covered. Don't waste your time.

russwoods
visit shbcf.ru