Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
18 Nov 2016CPOL2 min read 30.7K   3   2
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.

Image 1

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.

Image 2

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.

Image 3

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

Image 4

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.

Image 5

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 .

Image 6

Time to get back to WebAppProject, lets add WebApp.DAL as a project reference.

Image 7

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.

Image 8

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.

Image 9

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 

Image 10

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)


Written By
Software Developer
Philippines Philippines
I make art(games) for fun and write code for food.

Comments and Discussions

 
Questionremove all refs from WebApp project Pin
José Drumonde5-Jun-20 8:27
José Drumonde5-Jun-20 8:27 
QuestionViews Pin
Thomas Donino13-Oct-17 9:06
Thomas 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.