Xnitor
Xnitor

Reputation: 79

Database not created on startup

My database is no longer created when i start my application. We are four people working on the same project and it's only for me that the database is not created on startup. It was working before, i don't know what i changed. I have deleted the project several times and cloned it from our github. I have also restarted my computer and visual studio several times, but nothing is working. We have googled the problem but we can't solve it. Would appreciate if someone could help me out help. Is it possible that i have changed some options by mistake since it's only for me the database is not created?

Here is my IdentityDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("TurreKlippDb", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();

    }


    public DbSet<Categories> Categories { get; set; }
    public DbSet<Saloon> Saloon { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }

}

My DataInitializer

public class DataInitializer :
DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{

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



        // In Startup iam creating first Admin Role and creating a default Admin User    
        if (!roleManager.RoleExists("Admin"))
        {

            // first we create Admin rool   
            var role = new IdentityRole();
            role.Name = "Admin";
            roleManager.Create(role);
        }

        if (!roleManager.RoleExists("HairSaloon"))
        {

            // create HairSaloon rool   
            var role = new IdentityRole();
            role.Name = "HairSaloon";
            roleManager.Create(role);
        }


        if (!context.Users.Any(x => x.UserName == "[email protected]"))
        {
            PasswordHasher pwdHash = new PasswordHasher();
            string pwd = pwdHash.HashPassword("123");
            var saloon = new ApplicationUser();


            saloon.PasswordHash = pwd;
            saloon.UserName = "[email protected]";
            saloon.Email = "[email protected]";
            saloon.CompanyName = "AdminSaloon";

            saloon.SecurityStamp = Guid.NewGuid().ToString();
            context.Users.Add(saloon);
            context.SaveChanges();

            UserManager.AddToRole(saloon.Id, "Admin");
            context.SaveChanges();
        }




        var l = new List<ApplicationUser>();

        var lista = new List<String>();

        lista.Add("Herrklippning");
        lista.Add("Damklippning");
        lista.Add("Barnklippning");
        lista.Add("Ansiktsbehandlingar");
        lista.Add("Fransbehandlingar");
        lista.Add("Brynbehandlingar");
        lista.Add("Hudbehandlingar");
        lista.Add("Injektionsbehandlingar");
        lista.Add("Makeup");
        lista.Add("Hårborttaggning");
        lista.Add("Vaxningar");
        lista.Add("Naglar");


        foreach (var item in lista)
        {
            context.Categories.Add(
                new Categories
                {
                    CategoryName = item,
                    Saloons = l
                });
            context.SaveChanges();
        }
    }
}

And my Global.asax

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

My connection string

<connectionStrings>
    <add name="TurreKlippDb" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TurreKlippDb.mdf;Initial Catalog=TurreKlippDb;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Upvotes: 0

Views: 131

Answers (1)

Rajput
Rajput

Reputation: 2607

Your code seems to be fine (I hope you didn't get any compile or run time error ). Database will only be created if you will try to access object from your database.May be you didn't try to access any object from your database (eg: showing data on Index page or something). If you have already tried accessing your data and database didn't create then you will get error. If it is so then show us that error.

Upvotes: 1

Related Questions