0

I am working in a new MVC5 application that I built specifically to test this.

I have two db contexts. The configuration.cs for the ApplicationDbContext is as follows:

using FB.DOMAIN.Authentication;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity.Migrations;

namespace FB.DAL.Migrations.ApplicationDbContext
{
    internal sealed class Configuration : DbMigrationsConfiguration<DataContexts.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsDirectory = @"Migrations\ApplicationDbContext";
        }

        protected override void Seed(DataContexts.ApplicationDbContext context)
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new IdentityRole("Admin"));
            }

            var user = new ApplicationUser { UserName = "james@world.com" };


            if (userManager.FindByName("james@world.com") != null) return;

            var result = userManager.Create(user, "Password123!");

            if (result.Succeeded)
            {
                userManager.AddToRole(user.Id, "Admin");
            }
        }
    }
}

Shouldn't the above code seed my database with a user? Sadly it doesn't! When I query the AspNetUsers table, it is blank!

What am I doing wrong? :(

J86
  • 14,345
  • 47
  • 130
  • 228
  • I moved my Identity seed code within the app because OWIN wasn't available in the seed method if your context is derived from the IdentityDbContext. See http://stackoverflow.com/questions/23574591/seed-database-for-identity-2 – Steve Greene Mar 06 '15 at 19:49

1 Answers1

0

I couldn't get two separate contexts talking. So I followed LukeP's solution here.

A single context that houses all my related domain logic.

Community
  • 1
  • 1
J86
  • 14,345
  • 47
  • 130
  • 228