Click here to Skip to main content
15,885,213 members
Articles / Programming Languages / C#
Tip/Trick

Move Entity Framework Core v5.0 to a Separate Project

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
7 Apr 2021CPOL4 min read 9.7K   10   3
How to move Entity Framework Core v5.0 into a separate Visual Studio Project
A commercial application cannot have data access functionality mixed in the same layer and logic as where the display code resides. 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 wrote it up in case it could help anyone else.

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):

Image 1

2. Name the Web App 'EFCore5App'

Image 2

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.

Image 3

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

    Image 4

  • 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

    Image 5

  • file Startup.cs - registers DbContext ApplicationDbContext and Identity

    Image 6

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

Image 7

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

Image 8

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.

Image 9

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:

Image 10

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

Image 11

Copy model properties, Id and Name.

Image 12

14. Add Entity/Model to Migration

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

Image 13

15. Create Migration

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

    Image 14

  • Enter Add-Migration InitialCreate:

    Image 15

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

    Image 16

  • Enter Update-Database:

    Image 17

16. Complete

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

Image 18

History

  • 7th April, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPost VS update Pin
Roger Beall 20212-Jul-21 6:36
Roger Beall 20212-Jul-21 6:36 
QuestionWorked OK Pin
Member 1109503411-Apr-21 10:41
Member 1109503411-Apr-21 10:41 
QuestionWonderful example - is the code downloadable? Pin
MSBassSinger8-Apr-21 9:24
professionalMSBassSinger8-Apr-21 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.