.NET

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer

NAVIGATION - SEARCH

Merging ASP.NET Identity Data with Application Data

If you want to skip the background and fluff and got straight to the solution, click here.

Background

The new Identity model for ASP.NET provides a lot of benefit over the previous Membership framework. There is a great article discussing many of the details and how to get started at http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity.  Identity uses Entity Framework 6, and when you add a new project (MVC5 or WebForms 4.5), models and context are created for you.  A new connection is created using LocalDB (named “DefaultConnection”), and basic code (including the user interface) is scaffolded for you so you can get started right away without too much trouble.  Pretty slick!

I recently rewrote my home owner’s association website (http://www.rdhoa.net) with ASP.NET MVC5 and Entity Framework 6.1 taking advantage of the new Identity model. I used Code First, creating a context that did not use the default connection, but instead a new connection (still using LocalDB), since I didn’t want to mix my application data and Identity data.  This worked great until I went to publish my work to Azure.  LocalDB isn’t supported in Azure (for a number of reasons), and having two different LocalDBs in my application meant that I would have to create two different Azure databases to support my app.  While this is most likely the appropriate separation of concerns for larger applications, my little HOA site only has two users, and maybe 2 MB of data.  Seemed like a waste of Azure credits to me.

Merging the Databases

After some bing/googling, and spending more than my fair share of time on the ASP.NET/Identity site, it appeared that I would have to recreate the entire Identity data base in code to use my other connection, or change my context to use the default connection.  Neither of these options interested me, so I decided to just try something really basic.

The answer? Simply open the IdentityModels.cs file in the Models directory (this works for both MVC and WebForms) and change the constructor for the ApplicationDbContext to use your specific connection.

    public class ApplicationDbContext : 
           IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
      : base("name=RDHOAContext")
        {
        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

About the author

Philip Japikse

Philip JapikseA Principal Consultant/Architect with Strategic Data Systems (http://www.sds-consulting.com), international speaker, Microsoft MVP, ASPInsider, MCSD, CSM, and CSP, and a passionate member of the developer community, Phil Japikse has been working with .NET since the first betas, developing software for over 30 years, and heavily involved in the agile community since 2005. Phil is the Lead Director for the Cincinnati .NET User’s Group (www.cinnug.org) and the Cincinnati Software Architect Group, co-hosts the Hallway Conversations podcast (www.hallwayconversations.com), founded the Cincinnati Day of Agile (www.dayofagile.org), and volunteers for the National Ski Patrol. Phil enjoys to continuously learn new tech and is always striving to improving his craft. You can follow Phil on twitter via www.twitter.com/skimedic and read his blog at www.skimedic.com/blog, and see all of his connections at http://www.about.me/skimedic
Managed Windows Shared Hosting by OrcsWeb