MVC5 EF6 CodeFirst use two database for debug and release

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

Question

I have a project of intern website for my company. I use MVC5 with Entity Framework 6 on a Code First project.

It's an Intranet for my company so I want two database. 1 in production (release). 1 in dev (Debug).

I've edited the Web.config, Web.Debug.config and Web.Release.config files like that :

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

<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

<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.

My problem is that my site don't work cause the database IntranetRelease isn't updated from Intranet database.

I have made a perfect copy of Intranet and create manually IntranetRelease. It work but I can't do that when peoples will use the website cause all the data will be on IntranetRelease database.

I've tried in my project to do an Add-Migration xxx in release mode but it doesn't change anything.

The goal is this :

When the release intranet is in work, I work on the dev database. 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.

Thanks for help and sorry for my poor english.

Accepted Answer

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). 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)
  2. Create a ConncetionString in your Release.Config-File (use SlowCheeta, if App.Config this will be the Production Database)
  3. Create the DatabaseModel in Dev.
  4. Use Add-Migration "SomeNameGoesHere" to build an Migration from the current Database to your Model defined Database.
  5. When building Release, EF will take care for you to migrate the Release-Database to the desired (e.g. model-defined state). 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)

To read more about the Topic visit http://msdn.microsoft.com/en-us/data/jj591621.aspx




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why