Entity Framework Webapp

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
solution

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.
employee
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.

department

Now lets create EmployeeDBContext.cs class where we create datasets of tables to populate the data as applicable.
employeedbcontext

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.
employeerepository
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.
webform
Thats it for the Day..:)    Jazakallah khair

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s