数据库不会在第一时间创建 [英] Database is not getting created at first time

查看:167
本文介绍了数据库不会在第一时间创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用EF6重新创建数据库?

How to re-create the database using EF6?

我已尝试过以下两个帖子,但是我不知道为什么它不工作,并得到相同的错误。

I have tried both of following post but, i don't know why its not working and getting same error.


  1. 如何生成EF6数据库启用迁移,而不使用update-database?

  2. 迁移已启用上下文但数据库不存在或不包含映射表

  1. How do I generate an EF6 database with migrations enabled, without using update-database?
  2. Migrations is enabled for context ''but the database does not exist or contains no mapped tables

我已经在Web服务器上发布了我的示例。我正在使用 Sql Server 2012 Express DBMS。

I have already published my sample on the web server. I am using Sql Server 2012 Express DBMS.

当我创建我的应用程序并在Web服务器上发布它是正常的。它已经自动创建了数据库。之后,我在 ApplicationDBContext 中进行了一些更改,并在 IdentityModes(ApplicationUser)中添加了一些属性。然后我已经在我的本地IIS Express 上迁移了数据库,并且工作正常。然后我尝试再次在Web服务器上发布我的样本,但这次它显示一个错误。

When i have created my application and published it on web server it was working fine. It has created database automatically. After that i have made some changes in ApplicationDBContext and added some properties in IdentityModes(ApplicationUser). Then I have migrated database on my Local IIS Express and it was working fine. Then I have tried to publish my sample again on web server but, this time it shows an error.

Migrations is enabled for context 'ApplicationDbContext' but the database does 
not exist or contains no mapped tables. Use Migrations to create the database 
and its tables, for example by running the 'Update-Database' command from the 
Package Manager Console.

我已经删除了Web服务器上的数据库,并再次尝试。但是,我仍然面临同样的错误。现在有一个没有表的空数据库。

I have deleted the database on web server and tried again. but, still i am facing same error. Now, there is an empty database without table.

应用程序正在创建用户注册数据库。

Application is creating database on User Registration.

我有也阅读了这个 帖子 ,并尝试调用 dbContext.Database.Initialize (true); ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase()方法,但仍然不起作用。

I have also read this post and tried to call dbContext.Database.Initialize(true); and ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase() method but, still its not working.

推荐答案

首先,自动迁移对于开发来说是很好的,但它是一个非常重要的依靠他们的坏主意在生产中。第二,在某些时候你关闭自动迁移,这就是为什么你的生产数据库不再有在其中创建表的原因。

First, automatic migrations are nice for development but it's a hugely bad idea to rely on them in production. Second, at some point you turned off automatic migrations, which is why your production database is no longer having tables created in it.

继续,自动迁移;即使在开发中,通过生成迁移,查看要执行的代码,然后只运行更新数据库更好地了解您对数据库进行的更改,

Go ahead and leave automatic migrations off; even in development it's better to know exactly what changes you're making to your database by generating a migration, seeing the code that will be executed, and only then running Update-Database to apply those changes.

您有多种选择可以将模式更新转换为生产,但都将归结为大致相同的过程。

You have a number of choices in terms of getting schema updates into production, but they all boil down to roughly the same procedure.


  1. 根据您的本地数据库生成新的迁移。

  2. 如果要通过SQL更新生产数据库,请运行更新数据库-Script 。这将生成一个SQL文件,其中的代码在数据库上运行以迁移模式。如果您有自己的数据库,或者自己检查SQL代码,然后通过Management Studio将其手动应用到数据库,请将其关闭到您的DBA。

  3. 运行更新数据库。这次没有 -Script 来更改本地数据库模式。

  4. 如果您没有在步骤中生成SQL文件2,在Visual Studio中的SQL Server对象资源管理器中查看本地数据库。右键单击本地数据库,然后选择模式比较...。按照向导与您的生产数据库进行比较。最后,您可以生成一个需要进行必要更改的SQL文件(再次,如果有的话您将转交给DBA),或者您可以直接从Visual Studio迁移到生产。但是,这不是真的推荐。最好生成SQL,您或您的DBA可以在实际应用更改之前检查。

  1. Generate a new migration against your local database.
  2. If you want to update your production database via SQL, run Update-Database -Script. This will generate a SQL file with the code to run on the database to migrate the schema. Hand this off to your DBA if you have one or review the SQL code yourself and then apply it manually to your database via Management Studio.
  3. Run Update-Database. This time without -Script to make the changes to your local database schema.
  4. If you didn't generate the SQL file in step 2, view your local database in SQL Server Object Explorer in Visual Studio. Right-click on the local database there and choose, "Schema Compare...". Follow the wizard to compare with your production database. In the end, you can either generate a SQL file with the necessary changes that need to be made (which again, you'd hand off to your DBA if you have one), or you can migrate to production directly from Visual Studio. However, this is not really recommended. It's always best to generate the SQL, which you or your DBA can then inspect before applying the changes live.

这篇关于数据库不会在第一时间创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆