As a developer who enjoys writing applications with SOLID in mind, as much as possible I aim to move my DAL related objects to another project or assembly so I can easily test them and can be reused and shared at times that they'll be needed for another application.
ASP NET Core's default mvc application template (with single user auth),
IdentityDbContext is set within the project but separating it is not a difficult task.
Lets start with a fresh ASP NET Core Web application in Visual Studio.
Select Web Application as template, Click change authentication , set to Individual user accounts. This will install ASP NET Core Identity on our project and will be used for Authentication/authorization on our project.
Since Dot Net Core 1.1 is released, we will update our nuget packages to get the lastest packages for our project. Open up package manager and get all updates for our packages.
Now that our website is set up, time to create the DAL project to contain all of our DAL related objects. On visual studio solution folder, right-click and add new project. Select a class library(.net core) and name it WebApp.DAL
On WebApp project, select and cut/move Data folder via solution explorer.
Then paste it on WebApp.DAL project.
Now that we've moved the Data folder and its contents. Delete Migrations within Data folder.
Create Models folder then move WebApp/Models/ApplicationUser.cs to our WebApp.DAL/Models.
You'll end up to have files like this.
Visual studio may tell you that theres something wrong with ApplicationUser. To fix this we will hover over using Microsoft.AspNetCore.Identity.EntityFrameworkCore; and click light bulb button.
Click add Add Package.. on dialog to reference required packages on project.json .
Time to get back to WebAppProject, lets add WebApp.DAL as a project reference.
Hit rebuild solution, errors will pile up. but no need to get sad about it. You can now fix them one by one.The errors are caused by moving our objects to WebApp.DAL and can be easily resolved by clicking the Light bulb context icon like we what we did earlier.We then update WebApp's project.json, we'll add runtimes as seen below.
Once we've cleared all compile time errors, we can now run our website.But once you hit register or login you'll meet this error page.
Update WebApp.DAL's project.json, add EntityFrameworkCore.Tools on tools and set NETStandard.Library version to 1.6.1 (will show you on screenshot below). I also added Microsoft.EntityFrameworkCore.SqlServer as dependency.
When done, then open up package manager console. set default project to WebApp.DAL. then execute these commands on package manager console:
Now run our our website again in debug mode and were supposed to run it without problems.
If you have suggestions and things to point out on this post feel free to post your comment so i tahn i can address your concerns and improve this blog post.