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);
}
}
}