Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure EF Code First Migration Initializer

I am just messing with Azure, and I can't seem to get my Db to work. I am following what it says here: https://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/ and I updated my web.config to have this:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <contexts>
          <context type="DownloadThis.Models.DownloadThisDb, DownloadThisDb">
              <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion">
                  <parameters>
                      <parameter value="DownloadThisDb_DatabasePublish" />
                  </parameters>
              </databaseInitializer>
          </context>
      </contexts>
  </entityFramework>

As is shown in the example, but I keep getting this error:

Format of the initialization string does not conform to specification starting at index 0.

I have triple checked my connectionString, so that isn't it - any ideas?

like image 544
naspinski Avatar asked Jan 16 '23 20:01

naspinski


2 Answers

Assuming that you're publishing with the VS2012 publish wizard, I've run into the same issue. If you choose to have the publishing wizard enable code first migrations instead of manually wiring them up in your code, then you need to provide a connection string in the publish settings. Your normal connection string is not used to run the migrations, this new connection string is used for this purpose only. This is nice because you can specify an account that has elevated privileges for performing your migrations, however, your app won't run under this user context. The problem is that the wizard doesn't make the need to specify this connection string very obvious. When you don't supply this, you end up with a null migration connection string and you spend a lot of time trying to figure out what's wrong with your normal connection strings.

Publish Web Application Settings

Let's say that your context class is named FooContext. By convention, you'll have a connection string in your web.config named FooContext. When you enable code migrations via this wizard, the wizard will create a second connection string named FooContext_DatabasePublish that will only be used for running your code first migrations.

This blog post on MSDN explains this process in some detail.

like image 52
Joe Barone Avatar answered Jan 22 '23 18:01

Joe Barone


I think you're missing a . in your type string:

<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion">

The red rectangle over the code makes it hard to read...

If that doest fix it, post a comment and I'll work up a sample to match yours and see if I can get it to work...

[UPDATED 2012-08-15]

OK - I think I know what's going on here... You mentioned "I updated my web.config to have this:" and showed your XML. When I ran through the tutorial, I did NOT have to enter ANY extra XML into my web.config. During the publishing process, the XML was added for me automagically by Visual Studio's deployment process and it all "just worked".

Here's your solution:

Go back to the original web.config file without these updates, and try publishing again.

For reference, here are the <entityFramework> sections from my two web.config files, first from my project, second from my hosted service (I got that by connecting to the running site via FTP and downloading it). The VS project is called 11964172 after the SO record number for this post:

local web.config file settings

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

deployed web.config file settings

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <contexts>
      <context type="_11963331.Models.ToDoDb, 11963331">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[_11963331.Models.ToDoDb, 11963331], [_11963331.Migrations.Configuration, 11963331]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          <parameters>
            <parameter value="_11963331.Models.ToDoDb_DatabasePublish" />
          </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>

I guess that explains why they took a picture of the web.config file changes instead of actually providing the code to type in :-)

like image 26
Chris Koenig Avatar answered Jan 22 '23 18:01

Chris Koenig