Entity Framework Core 2.0 Repository Pattern

Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology.

EF Core is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write. Object-relational mapping is a technique that enables developers to work with data in object-oriented way by performing the work required to map between objects defined in an application’s programming language and data stored in relational database.

You can download the source code from here.

Prerequisites

  • Visual Studio 2017
  • .Net Core Framework 2.0

Setup projects in Visual Studio

  • Create a new solution in Visual Studio. Here I created a solution HiveStore.
  • Add a new .Net Core library project for Entity or model classes to the solution. Here I created a library project HiveStore.Entity.
  • Add a new .Net Core library project for data access related classes to the solution. In demo application HiveStore.DataAccess refers to this.
  • Add a new .Net Core console application to write the test code.
  • Below are the projects in the solutions
  1. HiveStore.TestConsole.
  2. HiveStore.Entity
  3. HiveStore.DataAccess

 

 

 

Setup Hive.Entity project

  • Add a BaseEntity class to Codify.Entity project. The BaseEntity contains common properties like CreatedDate, CreatedBy etc. which are part of all tables.

  • Add a class file for Employee entity.
  • Add a class file for Product entity

  • Add a class file for Order entity

  • Add a class file for Order Details Entity.

 

Setup Hive.DataAccess project

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

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

  • Create EmployeeRepository in Employee-> Repository folder.
  • Create OrderRepository, OrderDetailsRepository in Order -> Repository folder.
  • Create ProductRepository in Product -> Repository folder.

  • And finally lets create the DataContext class. Add a class file for HiveDataContext.cs in Hive.DataAccess project and add below code.

  • Change the connection string to point to your database in below method.

Creating and Managing Database Schemas

Migrations
Migrations provide a way to incrementally apply schema changes to the database to keep it in sync with your EF Core model while preserving existing data in the database.

Creating the Database
So we have created our model classes. Now lets create the database. To do this, add an initial migration. For this we need to Install the EF Core Tools using Package Manager console. Select HiveStore.DataAccess as the default project.

Run below command in Package Manager Console to create Migrations folder.

If this command does not generate Migration file add below setting in HiveStore.DataAccess.csproj file.

Run below command to create Database and tables.

DB Diagram

Setup Hive.TestConsole Project

  • Install Microsoft.EntityFrameworkCore.SqlServer Package using PMC.
  • Add below code in Program.cs file.

  • Execute the program and after successful run records are inserted in to [hive].[Employee] table.

You can download the source code from here.

One comment on “Entity Framework Core 2.0 Repository Pattern

Leave a Reply

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