C# 如何强制迁移使用触发迁移的客户端上下文的连接字符串

C# 如何强制迁移使用触发迁移的客户端上下文的连接字符串,c#,entity-framework,entity-framework-migrations,C#,Entity Framework,Entity Framework Migrations,应用程序使用EF 6.1.2连接到数据库。应用程序使用EF迁移功能自动升级数据库 但是,迁移确实调用无参数构造函数,而不是使用触发迁移的客户端上下文使用的连接字符串。如何强制迁移(在var z=await y.ToListAsync();行上触发)以使用提供给clientContext的连接字符串 客户端上下文的连接字符串由用户提供(即,它在app.config中或在任何其他名称下都不可用) 代码 Database.SetInitializer(new MigrateDatabaseToLate

应用程序使用EF 6.1.2连接到数据库。应用程序使用EF迁移功能自动升级数据库

但是,迁移确实调用无参数构造函数,而不是使用触发迁移的客户端上下文使用的连接字符串。如何强制迁移(在
var z=await y.ToListAsync();
行上触发)以使用提供给
clientContext
的连接字符串

客户端上下文的连接字符串由用户提供(即,它在app.config中或在任何其他名称下都不可用)

代码

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

var clientContext = new ClientContext(connectionString);

var y = clientContext.Set<TEntity>();
var z = await y.ToListAsync();

在数据库配置类中设置TargetDatabase

  public sealed class SomeConfiguration : DbMigrationsConfiguration<SomeDbContext>
    {
        public Configuration()
        {
            //Creates a new instance of DbConnectionInfo based on a connection string.
            TargetDatabase = new DbConnectionInfo(
 connectionString:"The connection string to use for the connection",
 providerInvariantName:"The name of the provider to use for the connection. Use 'System.Data.SqlClient' for SQL Server.");
        }
    }
公共密封类SomeConfiguration:DbMigrationsConfiguration
{
公共配置()
{
//基于连接字符串创建DbConnectionInfo的新实例。
TargetDatabase=新的数据库连接信息(
connectionString:“用于连接的连接字符串”,
providerInvariantName:“用于连接的提供程序的名称。对于SQL Server,请使用'System.Data.SqlClient'”;
}
}

将true传递给构造函数(将true传递给构造函数表示迁移应该重用触发迁移的客户端上下文)。无需手动向初始值设定项提供连接字符串

Database.SetInitializer(new MigrateDatabaseToLatestVersion<ClientContext, Configuration>(true));
Database.SetInitializer(新的MigrateDatabaseToLatestVersion(true));

我需要能够直接提供连接字符串,因为它不在app.config中(可能由用户提供)。
  public sealed class SomeConfiguration : DbMigrationsConfiguration<SomeDbContext>
    {
        public Configuration()
        {
            //Creates a new instance of DbConnectionInfo based on a connection string.
            TargetDatabase = new DbConnectionInfo(
 connectionString:"The connection string to use for the connection",
 providerInvariantName:"The name of the provider to use for the connection. Use 'System.Data.SqlClient' for SQL Server.");
        }
    }
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ClientContext, Configuration>(true));