I\'m trying to move IdentityModels.cs to another project to keep the web site apart from the Data Access Layer.
I followed this tutorial: http://blog.rebuildall.net/
I successfully did this today after reading several posts that didn't seem to quite have all the pieces I needed. I hope this helps someone else.
My Goal: move models from existing web project into a different project. This includes domain models, business logic, and ASP Identity models.
[EDIT: Somehow I missed that the question was for Web Forms. I did this in MVC. However, I believe most would still hold true from what I'm seeing in a VS2013 web form project.]
Step by step:
Added new class library to solution named xyz.Models (xyz is the existing web project’s namespace) – using something else like ModelLib is fine, you’ll just have to search/replace namespaces later whereas the former you won't.
From the web project, move all domain models to the class library. I included the database context class (exam. XyzContext.cs), all AspNet... models, and the IdentityModels.cs. Note: leave microsoft's default ManageViewModels.cs where it is for the moment.
Next, I moved the ManageViewModels.cs into my web project’s ViewModels folder and changed it’s namespace from Models to ViewModels. The existing cshtml files in Views/Manage need to reflect this namespace change as well.
Next, ManageViewModels.cs is used by ManageController.cs so I added ‘using xyz.ViewModels’ to ManageController.cs.
Next, with an empty Models folder in my web project, I excluded it from the project.
Next, from the web project’s App_Start, I moved the IdentityConfig.cs to the models class library and changed it’s namespace to xyz.Models (also removed its ‘using xyz.Models’ statement)
Next, I added the class library (xyz.Models) as a reference to the web project.
Next, I installed the following NuGet Packages into the Class Library
The following may not apply to your project, but these are other things I needed in the class library based on some business logic classes:
A reference to ‘System.Web.Mvc’
A reference to ‘System.Web’ – Note: it was necessary to add a project reference to System.Web because I was using HttpContextBase, HttpContext in the class library (this was confusing at first since my class already had a ‘using System.Web’ statement. I won’t hash out why here, but just make sure you have ‘System.Web’ in your project references (System.Web.Mvc alone won’t do).
While at it, as my own preference, I changed “DefaultConnection“ in my IdentityModels.cs to the database context that I am using for my others (and deleted the reference in my web project's web.config for DefaultConnection; keeping “XyzContext”.) Note: all tables are in same db.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("XyzContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
At this point, compiling gave me a 'GetOwinContext' error in one of my custom classes that I had created for centralizing some aspnet identity business logic. To resolve this I needed another NuGet package in my class library: Microsoft.Owin.Host.SystemWeb.
All worked fine after that.