Initialization of MVC4 EF database is incomplete in production
I created a website using MVC4 and I updated the framework to 4.5.1.
Everything works fine during the development phase. I first enabled the migration with entity framework code and created the "Init" migration.
I have a simple membership. When I delete my local database and run the application, I have all the tables created perfectly.
However, when I deploy (FTP) to a production server, I only have a subset of the tables.
This is what Global.asax.cs did after I tried this
internal void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<GlobalDbContext, Migrations.Configuration>("DefaultConnection"));
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
using (var context = new GlobalDbContext())
{
if (!context.Database.Exists())
{
// Register the SimpleMembership database without Entity Framework migration schema
//((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
context.Database.Create();
}
////context.Database.Initialize(true);
//context.Database.Delete();
//context.Database.Create();
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "Username", autoCreateTables: true);
}
Apparently It context.Database.Create();
behaves differently in production.
I have already deployed a complete MVC4 application and a complete MVC5 application. But this is the first time I am deploying an MVC4 application updated to .net 4.5.1 (I don't know if that helps).
Any idea?
Edit (add sample DbContext)
public class GlobalDbContext: DbContext {public GlobalDbContext (): base ("DefaultConnection") {}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<GlobalDbContext>
(
new CreateDatabaseIfNotExists<GlobalDbContext>()
);
//using model builder here
base.OnModelCreating(modelBuilder);
}
public DbSet<Patient> dbSet1 { get; set; }
[***]
}
List of items to check:
- Is 4.5.1 installed on the production server
-
Are the permissions the same for sql server on production server?
-
Add catch attempt on create database call
For additional issues with migrations see the following:
EntityFramework 6.0 CreateDatabaseIfNotExists Code to create database first
Make sure that the account that the application uses to log on to SQL Server has the necessary permissions to create objects on the database server.