Entity Framework Generic Repository

Introduction

JIN Weijie
Jul 25, 2017 · 3 min read

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

Maximizing Quality of Life, Being Earth Friendly.

JIN Weijie

Written by

Maximizing Quality of Life, Being Earth Friendly.

JIN Weijie

Maximizing Quality of Life, Being Earth Friendly.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade