Move Entity Framework Core v5.0 to a Separate Project (edit)

Move Entity Framework Core v5.0 to a Separate Project - CodeProject

Adaptive Hierarchical Knowledge Management - Part I - CodeProject

Adaptive Hierarchical Knowledge Management - Part II - CodeProject

A JavaScript Form Generator - CodeProject

Compare Two Databases, Find Differences and Update using Visual Studio - CodeProject

How to Create SQL Server Database Project With Visual Studio - CodeProject

Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

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.


Microsofts 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 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 Repositiry 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