Entity Framework Generic Repository

Introduction

JIN Weijie
JIN Weijie
3 min readJul 25, 2017

--

A generic Entity Framework repository pattern implementation.

Code

Usage

Unzip Database\Database.7z, attach to LocalDB, or restore to your favoriate Sql Server instance and change the connection string in App.Config.
Open solution and run the application.

Base Repository

The base repository exists in Common\AbstractRepository.cs, it handles the basic CRUD operations, for example, methods like Find with pagination, Create, Update, Delete.

The Find method, which has many overloading, handles pagination, sorting, total count. You can use this kind of generic methods for all the entities.

It supports async call and also in the parent-child table scenario, you can pass the include parameter to do the join table.

Derived Repository

For certain business logic, you may need derived repository from the base repository to handle more business logic.

In the sample, you may find Repository\LogRepository.cs which is a repository for Log. In this example, i don’t have additional business logic, so it’s just look like this:

And the in the caller code, you can just use it like:

--

--

JIN Weijie
JIN Weijie

Maximizing Quality of Life, Being Earth Friendly.