gpt4 book ai didi

c# - EF Code First MigrateDatabaseToLatestVersion 从配置中接受连接字符串名称

转载 作者:可可西里 更新时间:2023-11-01 07:44:34 26 4
gpt4 key购买 nike

在我的项目中尝试实现 EF 迁移时,我被困在一个地方。

EF Code First MigrateDatabaseToLatestVersion 接受来自配置的连接字符串名称。

在我的例子中,数据库名称在运行时已知(用户从下拉列表中选择它)。正如 DbContext 在其构造函数中接受 ConnectionString 或 connectionString Name 的方式,“MigrateDatabaseToLatestVersion”不接受相同的

System.Data.Entity.Database.SetInitializer
(new MigrateDatabaseToLatestVersion<SrcDbContext, SRC.DomainModel.ORMapping.Migrations.Configuration>(connString));

还有其他方法可以实现吗?

最佳答案

谢谢大家。我确实从 codeplex 中检查了 EF 代码,并在了解了他们的源代码后继承了我自己的类。这是我选择的解决方案:-

public class MigrateDbToLatestInitializerConnString<TContext, TMigrationsConfiguration> : IDatabaseInitializer<TContext>
where TContext : DbContext
where TMigrationsConfiguration : DbMigrationsConfiguration<TContext>, new()
{
private readonly DbMigrationsConfiguration config;

/// <summary>
/// Initializes a new instance of the MigrateDatabaseToLatestVersion class.
/// </summary>
public MigrateDbToLatestInitializerConnString()
{
config = new TMigrationsConfiguration();
}

/// <summary>
/// Initializes a new instance of the MigrateDatabaseToLatestVersion class that will
/// use a specific connection string from the configuration file to connect to
/// the database to perform the migration.
/// </summary>
/// <param name="connectionString"> connection string to use for migration. </param>
public MigrateDbToLatestInitializerConnString(string connectionString)
{
config = new TMigrationsConfiguration
{
TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient")
};
}

public void InitializeDatabase(TContext context)
{
if (context == null)
{
throw new ArgumentException("Context passed to InitializeDatabase can not be null");
}

var migrator = new DbMigrator(config);

migrator.Update();
}
}

public static class DatabaseHelper
{
/// <summary>
/// This method will create data base for given parameters supplied by caller.
/// </summary>
/// <param name="serverName">Name of the server where database has to be created</param>
/// <param name="databaseName">Name of database</param>
/// <param name="userName">SQL user name</param>
/// <param name="password">SQL password</param>
/// <returns>void</returns>
public static bool CreateDb(string serverName, string databaseName, string userName, string password)
{
bool integratedSecurity = !(!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(password));

var builder = new System.Data.SqlClient.SqlConnectionStringBuilder
{
DataSource = serverName,
UserID = userName,
Password = password,
InitialCatalog = databaseName,
IntegratedSecurity = integratedSecurity,
};


var db = new SrcDbContext(builder.ConnectionString);

var dbInitializer = new MigrateDbToLatestInitializerConnString<SrcDbContext, SRC.DomainModel.ORMapping.Migrations.Configuration>(builder.ConnectionString);

//following uses strategy to "CreateIfNotExist<>"
dbInitializer.InitializeDatabase(db);

return true;

}
}

关于c# - EF Code First MigrateDatabaseToLatestVersion 从配置中接受连接字符串名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14710758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com