Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateDatabaseIfNotExists Initializer #5

Open
fouimette opened this issue Oct 1, 2014 · 2 comments
Open

CreateDatabaseIfNotExists Initializer #5

fouimette opened this issue Oct 1, 2014 · 2 comments

Comments

@fouimette
Copy link

I am trying to get the database to be created ONLY if it does not exist but am not being successful. I only replaced DropCreateDatabaseAlways() and replaced it with CreateDatabaseIfNotExists(). Do you think that is all I have to do?

Here is what my ApplicationDbInitializer() now looks like:

public class ApplicationDbInitializer : CreateDatabaseIfNotExists<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        InitializeIdentityForEF(context);
        base.Seed(context);
    }

    //Create [email protected] with password=Admin@123456 in the Admin role        
    public static void InitializeIdentityForEF(ApplicationDbContext db)
    {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
        const string name = "[email protected]";
        const string password = "Admin@123456";
        const string roleName = "Admin";

        //Create Role Admin if it does not exist
        var role = roleManager.FindByName(roleName);
        if (role == null)
        {
            role = new ApplicationRole(roleName);
            var roleresult = roleManager.Create(role);
        }

        var user = userManager.FindByName(name);
        if (user == null)
        {
            user = new ApplicationUser { UserName = name, Email = name };
            var result = userManager.Create(user, password);
            result = userManager.SetLockoutEnabled(user.Id, false);
        }

        // Add user admin to Role Admin if not already added
        var rolesForUser = userManager.GetRoles(user.Id);
        if (!rolesForUser.Contains(role.Name))
        {
            var result = userManager.AddToRole(user.Id, role.Name);
        }
    }
}
@fouimette fouimette reopened this Oct 1, 2014
@fouimette fouimette reopened this Oct 1, 2014
@fouimette
Copy link
Author

Sorry, I am fairly new to Github.

I am looking for creating the database when the application starts and not when I register my first user.

@xivSolutions
Copy link
Contributor

I don't think you can do that, at least, not from within the code like this. However, under the current configuration, the database should be created when you login as the default admin user defined in the IdentityConfig.cs file.

There may be some way to script this behavior, or you could explore and see if you can find a way to call InitializeDatabaseForEF eariler in the start-up process (I have honestly never looked).

But the standard behavior for code-first here is to run the initialization when the Db is first accessed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants