3

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; }
    [***]
}
Community
  • 1
  • 1
Daniel
  • 9,312
  • 3
  • 48
  • 48
  • Can you post a sample of your `DbContext` class? It is likely that your `DbContext` is using a different Connection String, thus not creating tables for your entity classes, even the Membership Provider is able to create it's tables. – Claies Sep 04 '14 at 04:57
  • You're using `CreateDatabaseIfNotExists` strategy, so are you dropping your DB in Prod? If not, this will not do anything. You need to use `DropCreateDatabaseIfModelChanges` or `DropCreateDatabaseAlways` or your own custom DB initializer. Usually in PROD, you do not want to drop the DB ever, since you'll lose existing data, so you need to go with your custom initializer. Locally, dropping/recreating is not an issue. – Mrchief Sep 05 '14 at 19:19

2 Answers2

2

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

Community
  • 1
  • 1
Mike Beeler
  • 4,081
  • 2
  • 29
  • 44
0

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

Chris Clements
  • 168
  • 1
  • 10