Daniel
Daniel

Reputation: 9521

MVC4 EF code first database initialization incomplete in production

I have created a website using MVC4 and I updated the framework to 4.5.1.

Everything works perfectly in development phase. I have enabled the migration with entity framework code first and created a migration "Init".

I have simple membership. When I drop my local database and launch the application, I have all tables perfectly created.

Local database

However, when I deploy (FTP) on the production server, I only have a subset of tables.

Production database

This is what is done in Global.asax.cs 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's context.Database.Create(); that behaves differently in production.

I already have deployed a full MVC4 application and a full MVC5 application. But it is the first time I deploy a MVC4 application updated to .net 4.5.1 (I don't know if that helps).

Any idea?

Edit (add DbContext sample)

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; }
    [***]
}

Upvotes: 3

Views: 947

Answers (2)

Chris Clements
Chris Clements

Reputation: 198

Ensure the account that the application uses to log into the SQL Server has the required permissions to create objects on the database server.

Upvotes: 0

Mike Beeler
Mike Beeler

Reputation: 4101

A list of items to verify:

  1. Is 4.5.1 installed on the production server
  2. Are the permissions the same for sql server on the production server?

  3. Add a try catch around the create database call

See the following for additional issues with migrations

EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database

Upvotes: 2

Related Questions