A confusing connection problem using Entity framework and Migrations

By Mirek on (tags: EF, migrations, categories: code)

Recently I have stumbled upon a strange exception that raised in my application. It has led to an exception with a confusing message “Could not determine storage version; a valid storage connection or a version hint is required.” Thankfully I have solved this issue finally.

I have check that it also occurs on the latest version of Entity Framework which currently is 6.1.2. To reproduce this issue you need to set up few things in the project.

  1. Install Entity Framework ver. 6.1.2
  2. Add a simple POCO class. The structure of the domain is not important, since the problem is not related to the domain model. I simply used a class like this
    public class Product
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
    }
  3. Create a context class with two constructors as follows
    public class AppContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
     
        public AppContext() { }
     
        public AppContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }
    }
  4. Clear the connection string in app.config file so you have something like this
    <connectionStrings>
      <add name="AppContext" connectionString="" providerName="System.Data.SqlClient" />
    </connectionStrings>
  5. Enable migrations on the project.

Now you can go and place following lines in the place where the application starts.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppContext, Configuration>());
var context = new AppContext(@"Data Source=.\;Initial Catalog=TestAppDB;Integrated Security=true;");
context.Products.Load();

As you can see first we set the database initializer to be a MigrateToLatestVersion initializer, since we want our database be updated automatically. Next we create the EF context by providing the connection string to the constructor, since, let say, we asked the user to provide the connection string and want to use it instead of what is placed in the config file.

And here the exception arise when we try to query the context, like the last line of above code snippet. The message of the exceptions says that

Could not determine storage version; a valid storage connection or a version hint is required.

which is a bit strange, since we are sure the connection string is correct. The problem lies in the migration initializer, which by default takes the connection information from a context constructed using the default constructor or registered factory if applicable. So since we have cleared the connection string in the app.config file the initializer cannot connects to the database and throws the exception.
The simplest solution for this problem now is to use a different constructor for the MigrateToLatestVersion initializer which accepts a boolean parameter.

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppContext, Configuration>(true)); 

where passing true says to the initializer to use the connection information taken from the context.

Well simple thing, but not obvious enough, thus may cause a confusions a little bit.