Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
    [***]
}
like image 315
Daniel Avatar asked Jul 11 '26 00:07

Daniel


1 Answers

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

like image 167
Mike Beeler Avatar answered Jul 13 '26 15:07

Mike Beeler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!