Search code examples
asp.netasp.net-mvcentity-frameworkef-code-firstentity-framework-migrations

How to prevent EF from Creating database?



i am trying to upload my Asp.net Mvc website. i have used code first approach. now i create a database on server and upload my files to Cpanel. but when i open the website i get this error:

CREATE DATABASE permission denied in database 'master'.

I know this is becuase of EF but i dont know how to fix it.
i tried to make database initialization OFF but that didint help. then i tried to disable migration by this:

public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        ContextKey = "blah";
    }

But that didnt help too. can anyone help me?


Solution

  • CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.

      public SchoolDBContext(): base("SchoolDBConnectionString") 
                {
                    Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>());
    
                    //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>());
                    //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>());
                    //Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer());
                }
                public DbSet<Student> Students { get; set; }
                public DbSet<Standard> Standards { get; set; }
    

    Source:http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx