When I create a context with a default connection string (as read from the app.config
) the database is created and the migrations work - basically everything is in order. Whereas when the connection string is created programatically (using SqlConnectionStringBuilder
):
- database isn't created when the database is not present (scenario
A
); CreateDbIfNotExists()
creates the newest version of database model but the migration mechanisms are not invoked (scenarioB
).
In A
an exception is thrown when I wish to access the database, as - obviously - it isn't there. In B
database is created properly migration mechanisms are not called, as is the case in standard connection string.
app.config:
"Data Source=localhost\SQLEXPRESS;Initial Catalog=Db13;User ID=xxx;Password=xxx
"
builder:
sqlBuilder.DataSource = x.DbHost;
sqlBuilder.InitialCatalog = x.DbName;
sqlBuilder.UserID = x.DbUser;
sqlBuilder.Password = x.DbPassword;
initializer:
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<
MyContext,
Migrations.Configuration
>()
);
Specs: Entity Framework: 5.0, DB: SQL Server Express 2008
Question&Answers:os