Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing my database (package deploy) with Code First Migrations

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.

like image 982
Bronzato Avatar asked Nov 05 '12 11:11

Bronzato


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

Can we run SQL script using code First migrations?

This utility makes using Entity Framework Code-First much easier to manage running SQL scripts as part of the migrations or seed data. With this utility, we are running all the scripts as part of the migration without manually executing any of the scripts.

Can we use code first with existing database?

This step-by-step walkthrough provides an introduction to Code First development targeting an existing database. Code First allows you to define your model using C# or VB.Net classes. Optionally additional configuration can be performed using attributes on your classes and properties or by using a fluent API.


3 Answers

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.

like image 21
OdeToCode Avatar answered Oct 16 '22 17:10

OdeToCode


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

like image 181
Dominic St-Pierre Avatar answered Oct 16 '22 17:10

Dominic St-Pierre


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

like image 1
eduncan911 Avatar answered Oct 16 '22 18:10

eduncan911