3

I'm writing an ASP.NET Web Forms application under Visual Studio 2012. I have an Entity Framework Code First context class, which links my C# objects with the database. But this context was automatically generated using the Database First approach. I use such features as unique indexes and user aggregates in my database which AFAIK are not supported by the Code First of Model First approach in EF 5.

I'm using the one-click publish button for deploying my project. My database is recognized as an EF database and I see the "Execute Code First Migrations" checkbox, but it is grayed out:

Publish Web dialog

That's ok, since I use the Database First approach. But I want to use the "Update database" option, which appears for my default connection and simply to synchronize the database schema. As described here under "The dbDacFx Web Deploy Provider" section. How to do that?

If i understand correctly, I always can manually use Microsoft SQL Management Studio for this purpose. How to do that manually in this case?

Mikhail
  • 20,685
  • 7
  • 70
  • 146

2 Answers2

3

You can create a second connection string entry with a different name in the connectionStrings element in the Web.config file. It will show up in the publish dialog with the dbDacFx option.

The scenario is similar to deploying Code First without using migrations -- see http://msdn.microsoft.com/en-us/library/ee942158.aspx#deploy_code_first_without_migrations

tdykstra
  • 5,880
  • 2
  • 23
  • 20
3

Tom (tdykstra) is correct you will need to create a new connection string in web.config to publish your model with dbDacFx instead of using EF CF migrations.

The Web Publish dialog supports two types of publish mechanisms for databases:

  1. EF CF publishing via migrations
  2. Incremental DB schema publish via dbDacFx

If a connection string is associated with an EF CF context then on the publish dialog it will always show up with the Executed Code First Migrations checkbox. We wanted to keep the story simple and easy to explain, so we have not created any configuration options for this. For those advanced users who do not want to use the publish dialog you can create a publish profile (.pubxml) file with the correct contents and use that for publishing from the command line. In order to have the VS publish dialog support for this scenario you have to add a connection string to web.config.

In your case you have a connection string, DatabaseEntities, which is used by an EF CF context. You should create a new connection string in web.config, DatabaseEntities-publish, which will only be used during publishing. You'll need to make sure that the connection string which you are publishing to is in the textboxes for both DatabaseEntities as well as DatabaseEntities-publish.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
  • Thanks for clarification. Please, take a look on my related question: http://stackoverflow.com/questions/12201606/update-database-feature-not-working-in-publish-web-dialog-in-visual-studio-2012 – Mikhail Aug 30 '12 at 20:46
  • I can't get the web publish package to deploy the database. I have it set in the Publish SQL tab but it doesn't deploy at all. I am using database first with Visual Studio 2013. The first time I used the publish dialog it showed my database being deployed. Now when I open the publish dialog the database is never included in the publish. – Heinrich Oct 30 '15 at 21:02