简体   繁体   English

MVC4 EF代码首先在生产中初始化数据库初始化

[英]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. 我使用MVC4创建了一个网站,并将框架更新为4.5.1。

Everything works perfectly in development phase. 一切都在开发阶段完美运作。 I have enabled the migration with entity framework code first and created a migration "Init". 我首先使用实体​​框架代码启用了迁移,并创建了一个迁移“Init”。

I have simple membership. 我有简单的会员资格。 When I drop my local database and launch the application, I have all tables perfectly created. 当我删除本地数据库并启动应用程序时,我完全创建了所有表。

本地数据库

However, when I deploy (FTP) on the production server, I only have a subset of tables. 但是,当我在生产服务器上部署(FTP)时,我只有一个表的子集。

生产数据库

This is what is done in Global.asax.cs after I tried this 这是在我尝试这个之后在Global.asax.cs中完成的

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(); 显然,它是context.Database.Create(); that behaves differently in production. 在生产中表现不同。

I already have deployed a full MVC4 application and a full MVC5 application. 我已经部署了一个完整的MVC4应用程序和一个完整的MVC5应用程序。 But it is the first time I deploy a MVC4 application updated to .net 4.5.1 (I don't know if that helps). 但这是我第一次将MVC4应用程序部署到.net 4.5.1(我不知道是否有帮助)。

Any idea? 任何想法?

Edit (add DbContext sample) 编辑(添加DbContext示例)

public class GlobalDbContext : DbContext { public GlobalDbContext() : base("DefaultConnection") { } 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; }
    [***]
}

A list of items to verify: 要验证的项目列表:

  1. Is 4.5.1 installed on the production server 是否在生产服务器上安装了4.5.1
  2. Are the permissions the same for sql server on the production server? 生产服务器上的sql server的权限是否相同?

  3. Add a try catch around the create database call 在create database调用周围添加一个try catch

See the following for additional issues with migrations 有关迁移的其他问题,请参阅以下内容

EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database EntityFramework 6.0 CreateDatabaseIfNotExists代码首先创建数据库

确保应用程序用于登录SQL Server的帐户具有在数据库服务器上创建对象所需的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将EF Code First应用程序部署到生产数据库 - Deploying EF Code First Apps to Production Database MVC4代码首先强制数据库列名 - MVC4 code first force database column name MVC4 / EF /代码优先-在派生类中实例化ICollection时,InvalidCastException - MVC4 / EF / Code First - InvalidCastException when instantiating ICollections in a derived class MVC 5,EF 6和“数据库中的代码优先”存储过程 - MVC 5, EF 6, and “Code First from Database” Stored Procedures 未使用MVC3生成EF代码优先数据库 - EF Code First Database Not Generated using MVC3 首先将表添加到现有数据库代码MVC 5 EF6 - Adding a table to an existing Database code first MVC 5 EF6 EF代码未在数据库中首先生成MVC模型中的字节列表 - List of bytes in MVC model not generated in database by EF code first 如何将MVC4与EF5 Db结合起来第一个单独的项目 - How to combine MVC4 with EF5 Db First separate project 在生产中首先使列可为空 EF 6 代码 - make column nullable EF 6 Code first in production 如何使用EF5和数据库优先方法在ASP.NET MVC4 Web应用程序中重构web.config? - How to refactor web.config in an ASP.NET MVC4 Web App with EF5 and Database First approach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM