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:
Below is the screen I have:
Any help is greatly appreciated.
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With