65.9K
CodeProject is changing. Read more.
Home

Move Entity Framework Core v5.0 to a Separate Project

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (4 votes)

Apr 7, 2021

CPOL

4 min read

viewsIcon

10546

How to move Entity Framework Core v5.0 into a separate Visual Studio Project

Introduction

It is a fundamental architectural principle for a commercial application to not have data access functionality mixed in the same layer and logic as where the display code resides.

Microsoft's official tutorials mix them together in the same component, something no professional developer would do. Core 5 is fairly new, the only other tutorial I could find was all controlled from the CLI (surely 99% of us would just want to do it from within Visual Studio?).

As I had to do this recently, and it took a lot of going backwards and forwards until I found a route that worked, I thought I would write it up in case it is of help to anyone else.

Platform

  • Visual Studio 2019
  • .NET Core 5.0
  • SQL Server 2019

Create the Solution

1. Create Solution

Create a new ASP.NET Core Web App (Model View Controller):

2. Name the Web App 'EFCore5App'

3. Additional Information

  • for Target Framework select .NET 5.0 (Current)
  • for Authentication Type, select 'Individual Accounts'

Most commercial websites or extranets are going to require either authenticated admin users, or authenticated members, or both, so by selecting 'Individual Accounts' from the beginning, it causes the EF Core 5.0 dependencies to be included and makes the following steps easier.

4. Build and Run Solution

Check the solution builds and runs successfully.

5. The Web App will now Contain the Configuration for using EF Core 5.0

The pertinent aspects being:

  • file \Data\00000000000000_CreateIdentitySchema.cs has the migrations to create Identity (authenticated users/members) entities in the database
  • file \Data\ApplicationDbContext.cs holds the context for connecting to the database

  • file appsettings.json - holds the database connection string. I don't even bother with LocalDB and immediately replace it with a valid connection string to a SQL Server database

  • file Startup.cs - registers DbContext ApplicationDbContext and Identity

Apply Initial Migration

6. Go to SQL Server and Create Database 'efcore5db'

7. Apply the Initial Identity Migration

  • Go to Tools -> NuGet Package Manager -> Package Manager Console
  • Enter Update-Database

This will add the Identity and migrations tables to the database, and more importantly, confirm EF is configured correctly and migrations are working.

Create Separate Repository Project

8. Add Separate Repository Class Library

Add a new Class Library called Repository to the solution. This is where we are going to move all our EF Core functionality to.

Also, add a Project Reference to this project from the Web app.

Move EF Core to Repository Project

9. Move NuGet Packages to New Class Library

We will now start moving the EF Core 5.0 functionality from the Web app to the new Repository project.

  • Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution...

The following packages are installed for EFCore5App:

For each of the following three packages, select Repository and Install. Then select EFCore5App and Uninstall.

  • Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer

Also, install Microsoft.EntityFrameworkCore.Tools to Repository (leaving it still installed for the Web app).

10. Move Contents of Data Folder

Move all the contents of the \Data folder to the Repository class library and delete the Data folder in the Web app.

Update the namespaces in files:

  • 00000000000000_CreateIdentitySchema.cs
  • 00000000000000_CreateIdentitySchema.Designer.cs
  • ApplicationDbContextModelSnapshot.cs
  • ApplicationDbContext.cs

Replace the '.Data' part of the namespace with '.Repository', e.g., EFCore5App.Data.Migrations to EFCore5App.Repository.Migrations.

In file Startup.cs, add 'using EFCore5App.Repository;'.

11. Build and Run Solution

Build the solution and remove the 'using EFCore5App.Data;' that are no longer needed.

Add New Migration to Repository Project

12. Add New Migration

In the Repository class library, create a folder called 'entity'.

13. Create Data Entity/Model

Under entity, create the first data model called ContentType (remove the .entity from the namespace).

Copy model properties, Id and Name.

14. Add Entity/Model to Migration

In file ApplicationDbContext.cs, add a DbSet for ContentType.

15. Create Migration

  • Go to Tools -> NuGet Package Manager -> Package Manager Console
  • Change the Default Project to Repository (important!):

  • Enter Add-Migration InitialCreate:

    The new migration should now be added to the Repository project:

  • Enter Update-Database:

16. Complete

Table ContentType table should now have been created in the SQL Server database.

History

  • 7th April, 2021: Initial version