2

I have an ASP MVC 5 app with code first migrations enabled. Whenever I publish it to a server, it adds an extra connection string to the Web.Config file along with a "parameter" element, and it breaks the site until I replace it. It looks like:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <contexts>
        <context type="MyApp.DataLayer.MyContext, MyApp">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MyApp.DataLayer.MyContext, MyApp], [MyApp.Migrations.Configuration, MyApp]], EntityFramework, PublicKeyToken=b77a5c561934e089">
                <parameters>
                    <parameter value="MyContext_DatabasePublish" />
                </parameters>
            </databaseInitializer>
        </context>
    </contexts>
</entityFramework>
<connectionStrings>
    <add name="MyContext" connectionString="Data Source=SQLServerName;Initial Catalog=MyDB;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
    <add name="MyContext_DatabasePublish" connectionString="MyContext_DatabasePublish.ConnetionString" providerName="System.Data.SqlClient" />
</connectionStrings>

I presume that extra connection string is for the database migrations, is that right? All I have to do to get things working is replace the connection string with the one above it, so replace MyContext_DatabasePublish.ConnetionString with Data Source=SQLServerName;Initial Catalog=MyDB;Integrated Security=SSPI for example.

First, is that connection string even needed? This is all in dev, and my migrations were all done from my local machine.

If it is needed, is there a way to keep it from coming in as MyContext_DatabasePublish.ConnetionString and instead come in as the connection string that works so I don't have to manually replace it every time I publish an update?

Thank you in advance for any information and advice!

1 Answer 1

2

See post here:

https://blogs.msdn.microsoft.com/aspnetue/2012/06/12/deployment-in-visual-studio-2012-rc-using-entity-framework-code-first-migrations/

This occurs when you setup code first migrations so that you can define a privileged account to run your migrations without having to elevate the privileges of your normal context user.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm still not sure how to deploy without having to manually edit that connection string, but the link was helpful in educating me on where the connection string is coming from. Thank you!
Sorry, it talks about that in one of the linked articles from the page. You basically define the connection for it in your prod transform config.
Ahhh, I think I got it now. Thanks again!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.