Click here to Skip to main content
14,265,708 members

ASP NET Core , Moving IdentityDbContext and EF models to an external project

Rate this:
5.00 (2 votes)
Please Sign up or sign in to vote.
5.00 (2 votes)
18 Nov 2016CPOL
How Move IdentityDbContext and EF models to an external project on ASP NET Core.

Background

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.

Moving IdentityDbContext

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:

Add-Migration initializeDB
update-database 

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.

License

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

Share

About the Author

jicking
Software Developer
Philippines Philippines
I make art(games) for fun and write code for food.
I personally believe that DotNet Core is the future.

Comments and Discussions

 
QuestionViews Pin
Thomas Donino13-Oct-17 9:06
memberThomas Donino13-Oct-17 9:06 

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.

Article
Posted 18 Nov 2016

Tagged as

Stats

16.3K views
2 bookmarked