第一次没有创建数据库 [英] Database is not getting created at first time

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

问题描述

如何使用 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 数据库?
  2. 已为上下文''启用迁移,但数据库不存在或不包含映射表

我已经在网络服务器上发布了我的示例.我正在使用 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) 中添加了一些属性.然后我在我的 Local 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.

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

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.

继续关闭自动迁移;即使在开发中,最好通过生成迁移、查看将要执行的代码,然后运行 ​​Update-Database 来应用这些更改,从而准确了解您对数据库所做的更改.

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 更新生产数据库,请运行 Update-Database -Script.这将生成一个 SQL 文件,其中包含要在数据库上运行以迁移模式的代码.如果您有,请将其交给您的 DBA,或者自己查看 SQL 代码,然后通过 Management Studio 将其手动应用到您的数据库中.
  3. 运行更新数据库.这次没有 -Script 来更改本地数据库架构.
  4. 如果您没有在第 2 步中生成 SQL 文件,请在 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天全站免登陆