6

I have a VS2012 MVC4 solution (EF5). When coding, I use my personal computer with an SQL Express installed on the same machine. Each time I need to publish an update to the company, I create a package (.zip) and then import this package on the IIS of the company (not accessible from my dev computer). Each time I published my solution on the IIS server of the company, I recreate the entire database. That was in the past...

Now, I changed my solution and use the Code First Migrations. When needed (entities changed), I update my local database thanks to the 'Package Manager console' (Update-Database).

My question: what do I have to do to update the database on the IIS of the company?

I see the screenshot below on an explanation page (http://msdn.microsoft.com/en-us/library/dd465337.aspx) but I don't have the same screen when I open the publish wizard on my VS2012.

Below is the screen seen on a documentation page:

enter image description here

Below is the screen I have:

enter image description here

Any help is greatly appreciated.

3 Answers 3

7

You can enable that check box by editing the Properties\YourProject - WebDeploy.pubxml look for the PublishDatabaseSettings

<PublishDatabaseSettings>
  <Objects xmlns="">
    <ObjectGroup Name="Namespace.Models.YourDBClass" Order="1" Enabled="True">
      <Destination Path="your-connection-string-goes-here" />
      <Object Type="DbCodeFirst">
        <Source Path="DBMigration" DbContext="Namespace.Models.YourDBClass, AssamblyName" MigrationConfiguration="Namespace.Migrations.Configuration, Assambly" Origin="Convention" />
      </Object>
    </ObjectGroup>
  </Objects>
</PublishDatabaseSettings>

Change Namespace.Models.YourDBClass by your class that inherits DbContext, change Namespace.Migratins.Configuration to fit your migration configuration namespace and Assambly with your Assambly name.

Save and open the publish wizard you will have that check box.

Hope that help

3
  • Hi Dominic, Thanks for that answer. I had a really odd problem that, for no apparent reason, Web Deploy and Entity Framework Power Tools stopped seeing the DbContext. I edited as you showed and it fixed it. Thanks. Jun 7, 2013 at 12:40
  • This worked for me... only if I created a new Object Group.. It would not add to the "Default Connection" object group
    – Mike
    Oct 1, 2016 at 19:26
  • Indeed it's important here to have the full name of your DbContext class as the name of your ObjectGroup. Thanks ! Nov 9, 2017 at 14:20
5

I've seen this happen, too. I'm not sure what heuristics the deployment tools use to identify the presence of EF, but the option appears sporadically.

What the publish tools do is easy to replicate. You can specify a database initializer in your web.config file (or a transform in web.config.release) to run the migrations. It would look something like the following (for a transform):

 <entityFramework>
     <contexts xdt:Transform="Insert">
       <context type="PlantonDbContextName, PlantonAssemblyName">
         <databaseInitializer
           type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[
               [PlantonDbContextName, PlantonAssemblyName], 
               [PlantonConfigurationName, PlantonAssemblyName]
             ], EntityFramework" />
       </context>
     </contexts> 
 </entityFramework>

Generics are ugly in a configuration file, but you could also set the initializer in code: http://msdn.microsoft.com/en-us/library/hh829293(v=vs.103).aspx

Hope that helps.

1

Let me take a stab and ask a question:

Was this previously a VS2010 project? :-)

I ask because the conversion process does not work to upgrade a VS2010 to a VS2012 and keep the publishing profiles intact. Just too many edits.

Best bet is to delete any existing publishing profiles you have (files) and recreate new publishing profiles.

Make sure to have the proper EF context specified via Ode's answer first though. The publishing wizard needs to be able to see your context before it creates profiles.

Entity Framework Code First Data Migrations not working with VS2012 Web Deploy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.