It is a fundamental architectural principle for a commercial web application to not have data access functionality mixed in the same layer and logic that controls the display.
As usual, Microsofts tutorials just mix them in together, something no professional developer would ever do. Core 5 is fairly new and 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?). If anyone does know of a good tutorial I would be interested in taking a look.
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 would write it up in case it is of help to anyone else.
This was built using:
1. Create solution
Create a new ASP.NET Core Web App (Model View Controller)
2. Name the Web app 'EFCore5App'
3. Additional Information
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 wiring for using EF Core 5.0.
The pertinent aspects being:
6. Go to Sql Server and create database 'efcore5db'
7. Apply the initial Identity Migration
This will add the Identity and migrations tables to the database, and more importantly, confirm EF is configured correctly and migrations are working.
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.
Also add a Project Reference to this project from the Web app.
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.
The following packages are installed for EFCore5App:
For each of the following three packages, select Repository and Install. Then select EFCore5App and Uninstall.
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:
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
Remove the 'using EFCore5App.Data;' statements that are no longer needed. Check the solution builds and runs successfully.
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).
Copy model properties Id and Name.
14. Add Entity/Model to Migration
In file ApplicationDbContext.cs add a DbSet for ContentType.
15. Create Migration
The new migration should now be added to the Repository project:
16. Complete
Table ContentType table should now have been created in the Sql Server database.