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.
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.
- 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
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
For each of the following three packages, select Repository and Install. Then select EFCore5App and Uninstall.
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:
Replace the '
.Data' part of the namespace with '
In file Startup.cs, add '
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
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,
14. Add Entity/Model to Migration
In file ApplicationDbContext.cs, add a
15. Create Migration
ContentType table should now have been created in the SQL Server database.
- 7th April, 2021: Initial version