1

I have two contexts in my project, both work independently just fine, I can register users using the ApplicationDbContext and the rest of my data is persisted to the database using a separate context. I have already went down this road so if it was a bad idea, I'm already here and need to find a solution.

I want to add some users and roles. SO I am using some code from the Deploying to Azure tutorials. You will see in my AddUserAndRole method that I am using a different context than the configuration and seed method are using. How do I get around this to add these users to their respective roles?

namespace XxxXxx.Migrations
{
    using Microsoft.AspNet.Identity.EntityFramework;
    using XxxXxx.Models;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : 
             DbMigrationsConfiguration<XxxXxx.DataLayer.myOwnContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        bool AddUserAndRole(XxxXxx.Models.Account.ApplicationDbContext context)
        {
            IdentityResult ir;
            var rm = new RoleManager<IdentityRole> 
                    (new RoleStore<IdentityRole>(context));
            ir = rm.Create(new IdentityRole("admin"));
            var um = new UserManager<ApplicationUser>
                    (new UserStore<ApplicationUser>(context));
            var user = new ApplicationUser()
            {
                UserName = "xx.xxx@gmail.com"
            };
            ir = um.Create(user, "12341234");
            if (ir.Succeeded == false)
                return ir.Succeeded;
            ir = um.AddToRole(user.Id, "isAdmin");
            return ir.Succeeded;
        }


        protected override void Seed(XxxXxx.DataLayer.myOwnContext context)
        {
            AddUserAndRole(context);
        }
    }
}
Eric Bishard
  • 5,201
  • 7
  • 51
  • 75

1 Answers1

0

I finally was able to figure out how to do this. Let me quickly reset the stage.

1) I started a typical MVC project using Identity and registered a few users right away. 2) I then started using code-first to persist changes to the database, but I changed the context name in my configuration file to something other than ApplicationDbContext and continued to use that new context along with code-first for my site data for many months before getting back to customizing my users.

3) I finally got to a point where I wanted to make changes to my users and roles. but wasn't sure how to target that context.

Here were the steps I took:

I opened up my console and enabled migrations for a new context using -ContextTypeName and specifying a new Migrations Directory for the context:

Enable-Migrations -ContextTypeName ApplicationDbContext -MigrationsDirectory Migrations\ApplicationDbContext

Then I changed my IdentityModel.cs file and added the new changes:

Models/IdentityModel.cs (ApplicationUser class)

//Additonal Properties for User
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Local Code")]
public string LocalCode { get; set; }

Then I used add-migration in combination with -ConfigurationTypeName and the path of my other context

Add-Migration -ConfigurationTypeName ProjectNamespace.Migrations.ApplicationDbContext.Configuration "AdditionalUserFields"

Next all we need to do is persist those changes using Update-Database and -ConfigurationTypeName and again specify the path to the context.

Update-Database -ConfigurationTypeName ProjectNamespace.Migrations.ApplicationDbContext.Configuration

You could in theory do this with many different contexts in MVC, but I would not use more than two at any time in my projects, just because it's a bit confusing. I also don't know if there are any cons to doing this. The reason I did it was because I didn't like the idea of using code-first with my ApplicationDbContext which inherits from IdentityDbContext<ApplicationUser>. No specific reason other than it seemed weird to do so since my site data has nothing to do with my application users. I have been told there is nothing wrong with just using ApplicationDbContext for everything, ... after the fact

Hope this benefits someone. I did find another answer of StackOverflow that helped me out eventually. Here it is:

Stack Overflow Question: Multiple Db Contexts in the same db/application

Community
  • 1
  • 1
Eric Bishard
  • 5,201
  • 7
  • 51
  • 75