Assalamualaykum wr br..:)
Today we are discuss about the code first approach of EF where code is designed based on which database is generated with tables and keys.
Lets quick start with web application. Create New Project -> WebSite -> <NameApp>.
Create the two classes in separate files Employee and Department also add two more classes with names as EmployeeDBContext.cs and EmployeeRepository.cs as shown
Code the Employee class as shown below. Every Employee is associated with Department hence it is defined using Department property whose return type is Department and as it is available in both tables(Classes) hence it is referenced as Navigation Properties and remaining properties like Id, Firstname, secondname etc are Static Properties.
Now code the Department class as below. Every Department have Id, Name which will be Static Properties and each department holds multiple employees for which list is considered with return type as employee which is navigation properties.
Now lets create EmployeeDBContext.cs class where we create datasets of tables to populate the data as applicable.
To fetch and operate on tables data we need to have one generic class which is referred to as EmployeeRepository.cs as shown below. It has one Get method which populates the department class data and returns.
Finally add new Webform.aspx and drag and drop grid view control and also datasource control as shown below. Before you add webform compile your project to let EF create dlls which is used to reference in datasource control. In above image of employeerepository class we see Include(“Employees”). This Include method used to display Navigation Properties where in Department table Employees is the Navigation Property.
Thats it for the Day..:) Jazakallah khair