Reputation: 2124
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?
Upvotes: 12
Views: 24773
Reputation: 6499
Make sure your dbcontext using correct connection string Some thing similar like this
public class DBContext : IdentityDbContext<ApplicationUser>
{
public DBContext ()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
}
And in web.config
<add name="DefaultConnection" connectionString="Server=....;Connection Timeout=300;" providerName="System.Data.SqlClient" />
Upvotes: 0
Reputation: 5073
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>
Upvotes: 23
Reputation: 945
See here for auto-creating the .sdf with EF 4.1 and the SQL CE NuGet package (or a new MVC 3 project apparently):
Long story short: Create an empty App_Data folder - the sdf is auto created, but only if the folder it goes in is present.
Upvotes: 2
Reputation: 781
Check SQL Server Management Studio -> .\sqlexpress
That's where CF has been putting all my databases when I don't specify a connection string.
Upvotes: 3