C# 实体框架代码优先 - 如何为生产数据库运行 Update-Database

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15715292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:42:48  来源:igfitidea点击:

Entity framework code first - how to run Update-Database for production database

c#asp.net-mvcentity-framework

提问by ironman

I wanting to know how to run the 'Update-Database' command for a production database.

我想知道如何为生产数据库运行“Update-Database”命令。

The 'Update-Database' database works fine from my local machine, but how do I get this to work for production data?

'Update-Database' 数据库在我的本地机器上工作正常,但我如何让它适用于生产数据?

So, if I make a change to my application and then run the 'publish' through visual studio this works fine for the code side of things, but how do I run the 'Update-Database' command for the production data.

因此,如果我对我的应用程序进行更改,然后通过 Visual Studio 运行“发布”,这对于代码方面来说效果很好,但是我如何为生产数据运行“更新数据库”命令。

Hope this question makes sense...

希望这个问题有意义...

Thanks,

谢谢,

采纳答案by NSGaga-mostly-inactive

To add on what @David said already...

补充一下@David 已经说过的话......

Personally, I don't trust automatic updatesin 'live' scenarios, and I always prefer manual database administration (i.e. there is a problem with permissionsneeded to create or alter Db - not to mention shared hosting etc.) - but from what I've seen migrations are pretty solid when it comes to synchronizing (in fact, the only way to 'untie' them normally is to remove the Db and force full/fresh update).

就我个人而言,我不相信automatic updates“实时”场景,而且我总是更喜欢手动数据库管理(即permissions需要创建或更改数据库存在问题- 更不用说共享托管等) - 但从我所看到的在同步方面,迁移非常可靠(实际上,通常“解开”它们的唯一方法是删除 Db 并强制完整/全新更新)。

Here is a post I made a while ago on how to scriptand synchronize database / codeand geared towards deployment scenarios (and when problems arise). It doesn't apply to you (yet) but something to keep in mind.

这里是我做了前段时间在一立柱如何scriptsynchronize database / code朝部署方案(当出现问题)为目标。它不适用于您(还),但需要记住一些事情。

MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created"

MVC3 和 Code First 迁移 - “自创建数据库以来,支持 'blah' 上下文的模型已更改”

回答by David Moore

See Using Entity Framework (code first) migrations in productionso that your application automatically updates the database when the Entity Framework initializes.

请参阅在生产中使用实体框架(代码优先)迁移,以便您的应用程序在实体框架初始化时自动更新数据库。

Now if you're more comfortable having manual control over the migration, you could use the -Script argument to the Update-Database command on your developer machine to generate SQL scripts which you can then run against the production database.

现在,如果您更愿意手动控制迁移,您可以在开发人员机器上使用 Update-Database 命令的 -Script 参数来生成 SQL 脚本,然后您可以针对生产数据库运行这些脚本。

http://msdn.microsoft.com/en-us/data/jj591621.aspx(see Getting A SQL Script section)

http://msdn.microsoft.com/en-us/data/jj591621.aspx(参见获取 SQL 脚本部分)

回答by Florian Winter

Do you just want to automaticallyupdate the database to the latest version when and where ever your application runs (development and production)?

您是否只想在您的应用程序运行(开发和生产)的时间和地点自动将数据库更新到最新版本?

This may not be a good idea, except in very simple scenarios where you know you can trust automatic migration and manually migrating the database(s) is not feasible. Please see this answer: https://stackoverflow.com/a/15718190/2279059. If you disregard this warning, read on.

这可能不是一个好主意,除非在您知道可以信任自动迁移并且手动迁移数据库不可行的非常简单的场景中。请参阅此答案:https: //stackoverflow.com/a/15718190/2279059。如果您忽略此警告,请继续阅读。

Add the following to web.config:

将以下内容添加到web.config

<entityFramework>
<contexts>
  <context type="MyAssembly.MyContext, MyAssembly" disableDatabaseInitialization="false">
    <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyAssembly.MyContext, MyAssembly], [MyAssembly.Migrations.Configuration, MyAssembly]], EntityFramework" />
  </context>
</contexts>

This may look scary, but it does basically the same as the following code:

这可能看起来很吓人,但它与以下代码基本相同:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());

If you have no luck with web.config, then you may also try to put this code into Global.asax. I personally prefer configuration over code.

如果您没有运气web.config,那么您也可以尝试将此代码放入Global.asax. 我个人更喜欢配置而不是代码。

If you want your config file to look cleaner, you can also derive a new class from the template MigrateDatabaseToLatestVersionclass so you don't need to use the cryptic syntax for passing type arguments in your web.cofigfile:

如果你想让你的配置文件看起来更简洁,你还可以从模板MigrateDatabaseToLatestVersion类派生一个新类,这样你就不需要使用神秘的语法在web.cofig文件中传递类型参数:

public class MyDatabaseInitializer : public MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration> {}

What this does is to set a database initializer which automatically updates the database to the latest version.

这样做是设置一个数据库初始化程序,它会自动将数据库更新到最新版本。

(Source and more details: http://www.ralphlavelle.net/2012/09/entity-framework-code-first-webconfig.html)

(来源和更多详细信息:http: //www.ralphlavelle.net/2012/09/entity-framework-code-first-webconfig.html