4

I am trying to set up some seed data for my MVC 5 Web Application, but it doesn't seem to be creating any for IdentityUser. When I check the App_Data folder it's empty (Show All files is enabled)

Here is my WebAppDatabaseInitializer.cs

public class WebAppDatabaseInitializer : DropCreateDatabaseIfModelChanges<WebAppDbContext> 
{
    protected override void Seed(WebAppDbContext context)
    {
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

        string name = "Admin";
        string password = "123456";
        string test = "test";

        //Create Role Test and User Test
        RoleManager.Create(new IdentityRole(test));
        UserManager.Create(new ApplicationUser() { UserName = test });

        //Create Role Admin if it does not exist
        if (!RoleManager.RoleExists(name))
        {
            var roleresult = RoleManager.Create(new IdentityRole(name));
        }

        //Create User=Admin with password=123456
        var user = new ApplicationUser();
        user.UserName = name;
        var adminresult = UserManager.Create(user, password);

        //Add User Admin to Role Admin
        if (adminresult.Succeeded)
        {
            var result = UserManager.AddToRole(user.Id, name);
        }
        base.Seed(context);
    }
}

and my Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        Database.SetInitializer(new WebAppDatabaseInitializer());
    }
}

any ideas what might be going wrong?

H H
  • 263,252
  • 30
  • 330
  • 514
teh0wner
  • 1,393
  • 6
  • 16
  • 33
  • 1
    Does something go wrong? Can you log in as Admin? Is Seed() hit in the debugger? – H H Nov 02 '13 at 18:30
  • When trying to login with that account I get an error: Cannot attach the file 'D:\Libraries\Documents\Visual Studio 2013\Repo\WebApp\WebApp\WebApp.Web\App_Data\aspnet-WebApp-20131019024822.mdf' as database 'aspnet-WebApp-20131019024822'. – teh0wner Nov 02 '13 at 20:00
  • Here's the stacktrace: http://pastebin.com/TDW3MZWw – teh0wner Nov 02 '13 at 20:02
  • I can't spot anything relevant beyond "provider failed to open". Maybe try to reproduce with a simple Db (not Identity) first. – H H Nov 02 '13 at 20:41
  • Even disabling the Seed and trying to register a User gives me the same error with the source being: Line 79: { Line 80: var user = new ApplicationUser() { UserName = model.UserName }; Line 81: var result = await UserManager.CreateAsync(user, model.Password); Line 82: if (result.Succeeded) Line 83: { – teh0wner Nov 02 '13 at 23:14
  • It's not the Seed() that's at fault but your Db config (Connection string etc) – H H Nov 03 '13 at 08:57
  • Indeed! There was no database in App_Data, however there was one in SQL Server Object Explorer which when I removed from localdb, it seemed to have fixed the issue. The error is now gone but still it doesn't seem to be creating anything in the database (from the seed).. Registering users normally works like a charm – teh0wner Nov 03 '13 at 11:32
  • It's rather easy to end up with 2 databases. But it's debuggable. – H H Nov 03 '13 at 12:55
  • I tried to breakpoint in the DB Initiliazer but it doesn't seem to run it.. Even tried changing to DropCreateDatabaseAlways but again it doesnt seem to run the seed methos – teh0wner Nov 03 '13 at 20:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40468/discussion-between-teh0wner-and-henk-holterman) – teh0wner Nov 03 '13 at 20:16

1 Answers1

3

The issue was that the DbContext that was being initialized was different that the one I used in ApplicationUser hence why it wasn't working.

teh0wner
  • 1,393
  • 6
  • 16
  • 33
  • 2
    could you elaborate on that? I am having a similar issue. I have both the Migrations and the DbContext Seed methods implemented yet none of them is being triggered. – Lord of Scripts Jul 21 '14 at 20:46