Entity Framework Repository Pattern Demo Application – Part 2

You can download the source code here.

Now lets create Repository classes and DataContext class. For this we need to install EntityFramework package using nuget.
Open Package Manager Console from Tools -> Nuget Package Manager and issue below command in console.

The structure of this project is as below. Configuration folder contains configuration settings for each model.Repository folder contains interfaces and implementation for each repository. Here one model is mapped to one repository.

Create an abstract BaseRepository class at the project root. This base class is inherited by all repository classes.

Create an abstract BasicEntityConfiguration class in the project root. This contains column level constraints for properties in BaseEntity.

Add EmployeeConfiguration class to Employee -> Configuration folder.

Add OrderConfiguration class in Order-> Configuration folder.

Add ProductConfiguration class in  Product-> Configuration folder.

Add OrderDetailsConfiguration in Order -> Configuration.

Create IEmployeeRepository in Employee-> Repository -> Interface folder.
Create IOrderRepository, IOrderDetailsRepository in Order -> Repository -> Interface folder.
Create IProductRepository in Product -> Repository -> Interface folder.

Add below code in IEmployeeRepository.

Create EmployeeRepository class in Employee -> Repository folder.

And finally lets create  the DataContext class. Add a class file for CodifyDataContext and add below code.

Create a new console project Codify.TestConsole to test the code and insert a few data into employee table. Below is the content in the Program.cs file.

Install EntityFramework in the TestConsole project.
Edit App.config and add the connection string to the database.

Now run the console application and database will be created by the entity framework.

One comment on “Entity Framework Repository Pattern Demo Application – Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *