[英]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: 要验证的项目列表:
Are the permissions the same for sql server on the production server? 生产服务器上的sql server的权限是否相同?
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.