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

2

9

I have created an MVC 3.0 application using Visual Studio 2012, .NET 4.5 and Entity Framework 5.0.

Using Code First Data Migrations, I am able to correctly propagate model changes to my local test database, but I can't figure out how to get this to work when deploying to my staging and production servers using Web Deploy.

I have read the following article ...

http://msdn.microsoft.com/en-us/library/dd394698(v=vs.110)#dbdacfx

... which explains what's supposed to happen, but it's not working for me, as Web Deploy seems unable to detect that I am using Entity Framework. The tutorial shows a checkbox to enable execution of Code First Migrations ...

Web Deploy Publish Dialog

... but my dialog shows the only Update Database checkbox for each database.

I have read that, in order for Visual Studio to detect the use of an Entity Framework context, the Web.config must include an element that defines it. Here's mine:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

    <contexts>
        <context type="MyContext, MyAssembly">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyContext, MyAssembly], [MyConfig, MyAssembly]], EntityFramework">
                <parameters>
                    <parameter value="MyConnectionStringName"/>
                </parameters>
            </databaseInitializer>
        </context>
    </contexts>
</entityFramework>

Any suggestions would be greatly appreciated.

Thanks,

Tim

Octans answered 20/9, 2012 at 14:12 Comment(0)
O
3

I have discovered the solution to this problem through experimentation.

In fact, my MVC application was created by VS2012 by converting from a VS2010 solution. Since the conversion process did not report any issues, I assumed that it had correctly converted everything, including the publishing profiles.

However, I discovered that the problem was in the conversion of these profiles and, unless I manually edit their XML files, there is apparently no way to get old imported profiles to participate in Code First Migrations.

Simply creating new publishing profiles in the converted solution results in the expected behavior.

Octans answered 22/9, 2012 at 15:0 Comment(2)
Care to give us a rundown of the differences?Ianthe
@Richard: within <PublishDatabaseSettings> the newly-created profile contains a number of additional <ObjectGroup> elements, each of which represents a context class. I assume there is some kind of discovery process that runs the first time Web Deploy is executed, which is not executed as part of the VS2012 solution conversion.Octans
O
0

Try this: Enable-Migrations -Force

Ensure the below is set to true AutomaticMigrationsEnabled = true;

Republish - worked for me

Outshoot answered 23/1, 2014 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.