当我使用默认连接字符串(从中读取app.config)创建上下文时,将创建数据库并且迁移工作 - 基本上所有内容都是有序的.而以编程方式(使用SqlConnectionStringBuilder)创建连接字符串时:
A);CreateDbIfNotExists()创建最新版本的数据库模型,但不调用迁移机制(方案B).在A我希望访问数据库时抛出异常,因为 - 显然 - 它不在那里.在B数据库中创建正确的迁移机制不会被调用,就像标准连接字符串中的情况一样.
app.config:" Data Source=localhost\\SQLEXPRESS;Initial Catalog=Db13;User ID=xxx;Password=xxx"
建设者:
sqlBuilder.DataSource = x.DbHost;
sqlBuilder.InitialCatalog = x.DbName;
sqlBuilder.UserID = x.DbUser;
sqlBuilder.Password = x.DbPassword;
Run Code Online (Sandbox Code Playgroud)
初始化程序:
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<
MyContext,
Migrations.Configuration
>()
);
Run Code Online (Sandbox Code Playgroud)
规范:实体框架:5.0,DB:SQL Server Express 2008
entity-framework connection-string ef-code-first ef-migrations
我有一个自定义DatabaseInitialiser,如下所示
/// <summary>
/// Implements the IDatabaseInitializer to provide a custom database initialisation for the context.
/// </summary>
/// <typeparam name="TContext">TContext is the DbContext</typeparam>
public class ParikshaDataBaseInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : DbContext
{
/// <summary>
/// The method to Initialise the database.
/// Takes care of the database cannot be dropped since it is in use problem while dropping and recreating the database.
/// </summary>
/// <param name="context">The DbContext on which to run the initialiser</param>
public void InitializeDatabase(TContext context) …Run Code Online (Sandbox Code Playgroud) c# entity-framework ef-code-first ef-migrations entity-framework-6
c# ×1