简体   繁体   English

MVC5 EF6 CodeFirst使用两个数据库进行调试和发布

[英]MVC5 EF6 CodeFirst use two database for debug and release

I have a project of intern website for my company. 我有一个为我公司提供实习生网站的项目。 I use MVC5 with Entity Framework 6 on a Code First project. 我在Code First项目中使用MVC5和Entity Framework 6。

It's an Intranet for my company so I want two database. 这是我公司的内联网,所以我想要两个数据库。 1 in production (release). 1在生产(发布)。 1 in dev (Debug). 1在dev(调试)中。

I've edited the Web.config, Web.Debug.config and Web.Release.config files like that : 我编辑了Web.config,Web.Debug.config和Web.Release.config这样的文件:

Web.Config Web.Config中

<connectionStrings>
    <add name="Connexion" connectionString="Data Source=[MYSQLSERVER];Initial Catalog=Intranet;Persist Security Info=True;User ID=[MYUSER];Password=[MYPASS]" providerName="System.Data.SqlClient"/>
</connectionStrings>

Web.Debug.Config Web.Debug.Config

<connectionStrings>
    <add name="Connexion" connectionString="Data Source=[MYSQLSERVER];Initial Catalog=Intranet;Persist Security Info=True;User ID=[MYUSER];Password=[MYPASS]" providerName="System.Data.SqlClient"
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Web.Release.config Web.Release.config

<connectionStrings>
    <add name="Connexion" connectionString="Data Source=[MYSQLSERVER];Initial Catalog=IntranetRelease;Persist Security Info=True;User ID=[MYUSER];Password=[MYPASS]" providerName="System.Data.SqlClient"
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

When I deploy my project using Web Deploy in release mod, all the job is done and the final Web.Config have good value IntranetRelease for the catalog. 当我在发布mod中使用Web Deploy部署我的项目时,所有工作都已完成,最终的Web.Config对目录具有良好的IntranetRelease值。

My problem is that my site don't work cause the database IntranetRelease isn't updated from Intranet database. 我的问题是我的网站不起作用导致数据库IntranetRelease未从Intranet数据库更新。

I have made a perfect copy of Intranet and create manually IntranetRelease . 我已经制作了完整的Intranet副本并手动创建IntranetRelease It work but I can't do that when peoples will use the website cause all the data will be on IntranetRelease database. 它工作但我不能这样做当人们将使用该网站导致所有数据将在IntranetRelease数据库。

I've tried in my project to do an Add-Migration xxx in release mode but it doesn't change anything. 我已经尝试在我的项目中在发布模式下执行Add-Migration xxx但它没有改变任何东西。

The goal is this : 目标是这样的:

When the release intranet is in work, I work on the dev database. 当发布内部网正在工作时,我在dev数据库上工作。 When all my changes are good for make a release, I want to deploy a new version with Web Deploy and do something to update the IntranetRelease database with my changes without lost any data on it. 当我的所有更改都适合发布时,我想部署一个带有Web Deploy的新版本,并使用我的更改do something更新IntranetRelease数据库,而不会丢失任何数据。

Thanks for help and sorry for my poor english. 感谢您的帮助,抱歉我的英语不好。

You can use Migrations for that, but you have to be Aware what a Migration actually does. 您可以使用迁移,但您必须意识到迁移实际上做了什么。
So you have two Databases Dev and Release, where the Dev Database has the current state and the release database has some state probably older than that. 因此,您有两个数据库开发和发布,其中开发数据库具有当前状态,并且发布数据库具有可能比其更旧的某个状态。

You create a Migration on the Dev-Database and then you run the code on the release database (I'd recommend not to do this from the web-Site, since it needs rights for using ddl-commands). 您在Dev-Database上创建迁移,然后在发布数据库上运行代码(我建议不要从Web站点执行此操作,因为它需要使用ddl-commands的权限)。 You can archieve this for example by running a console program, which checks and updates the database. 例如,您可以通过运行控制台程序来检查和更新数据库。

So how to do it in some simple steps: 那么如何通过一些简单的步骤来完成它:

  1. Create a ConnectionString in your Config-File (this will be the Developer-Database) 在Config-File中创建一个ConnectionString(这将是Developer-Database)
  2. Create a ConncetionString in your Release.Config-File (use SlowCheeta, if App.Config this will be the Production Database) 在Release.Config-File中创建一个ConncetionString(使用SlowCheeta,如果App.Config这将是生产数据库)
  3. Create the DatabaseModel in Dev. 在Dev中创建DatabaseModel。
  4. Use Add-Migration "SomeNameGoesHere" to build an Migration from the current Database to your Model defined Database. 使用Add-Migration“SomeNameGoesHere”构建从当前数据库到模型定义数据库的迁移。
  5. When building Release, EF will take care for you to migrate the Release-Database to the desired (eg model-defined state). 在构建Release时,EF将负责将Release-Database迁移到所需的(例如,模型定义的状态)。 DO NOT USE UPDATE-DATABASE FOR PRODUCTION DATABASE. 请勿使用更新数据库进行生产数据库。

Use Up() and Down() Functions to preserve Data during a Migration (EF will detect, if you would loose data and will warn you) 使用向上()和向下()函数在迁移期间保留数据(EF将检测,如果您将丢失数据并将警告您)

To read more about the Topic visit http://msdn.microsoft.com/en-us/data/jj591621.aspx 要阅读有关主题的更多信息,请访问http://msdn.microsoft.com/en-us/data/jj591621.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM