Entity Framework Code First Doesn't Generate Database

Viewed 24669

I created a db Context class and added a connection string in my web.config file as instructed in Scott Guthrie's Code First Development with Entity Framework 4. I am running it from a test method. I received several database errors running the tests, but when I finally cleaned up the classes so the test succeeded, I still had no database in the App_data folder.

I added Database.CreateIfNotExists() to the dbContext constructor, but still no sdf file. Anyone know what I am doing wrong?

4 Answers

For the database to be automatically created, the connection string name has to be named exactly as the DbContext subclass name (with namespace).

Eg. Say your DB class is like this:

namespace MyNamespace
{
    public class FooDb : DbContext
    {    
        public DbSet<XXX> ABC{ get; set; }
    }
}

Your connection string should look like so:

  <connectionStrings>
    <add name="MyNamespace.FooDb" connectionString="Data Source=|DataDirectory|MyNamespace.FooDb.sdf" providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>
Related