如何将连接字符串注入IDbContextFactory <t>?</t>的实例中

时间:2013-09-11 06:51:36

标签: c# entity-framework asp.net-mvc-4 dependency-injection unity-container

我正在使用Entity Framework 5和Code First Migrations。我有一个DataStore类,派生自DbContext

public class DataStore : DbContext, IDataStore
{
    public int UserID { get; private set; }

    public DataStore(int userId, string connectionString) : base(connectionString)
    {
        UserID = userId;
    }

    public virtual IDbSet<User> Users { get; set; }

    // Rest of code here
}

一个工厂类,它创建DataStore类的实例:

public class DataStoreFactory : Disposable, IDataStoreFactory
{
    private DataStore _database;
    private int _userId;
    private string _connectionString;

    public DataStoreFactory(int userId, string connectionString)
    {
        _userId = userId;
        _connectionString = connectionString;
    }

    public IDataStore Get()
    {
        _database = new DataStore(_userId, _connectionString);
        return _database;
    }

    protected override void DisposeCore()
    {
        if (_database != null) _database.Dispose();
    }
}

这些类的构造函数参数在运行时使用Unity注入。到目前为止一切都那么好,一切都很棒!

当我们进行迁移时出现问题:因为我的DataStore上下文类没有默认构造函数,所以我需要提供IDbContextFactory<T>的实现,以便Code First Migrations可以实例化它:

public class MigrationDataStoreFactory : IDbContextFactory<DataStore>
{
    public DataStore Create()
    {
        // Need to inject connection string so we can pass it to this constructor
        return new DataStore(0, "CONNECTION_STRING_NEEDED_HERE"); 
    }
}

问题是我无法弄清楚如何将连接字符串注入此类。我无法创建一个带有连接字符串参数的新构造函数,如下所示:

public class MigrationDataStoreFactory : IDbContextFactory<DataStore>
{
    public string _connectionString { get; set; }

    public MigrationDataStoreFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public DataStore Create()
    {
        return new DataStore(0, new DateTimeProvider(() => DateTime.Now), _connectionString);
    }
}

如果我这样做,我会在迁移时在运行时抛出以下异常:

[InvalidOperationException: The context factory type 'MigrationDataStoreFactory' must have a public default constructor.]
    System.Data.Entity.Infrastructure.DbContextInfo.CreateActivator() +326
    System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config,     DbConnectionInfo connectionInfo) +106
    System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType) +52
    System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext) +202
    System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) +66
    System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +50
    // Truncated stack trace, but you get the idea

除此之外,这个类还没有被Unity实例化;似乎只是通过Code First Migrations以某种方式召集,所以即使我能做到这一点也不会真的有帮助......

如果我在该方法中对连接字符串进行硬编码,一切正常,但出于显而易见的原因,我不想这样做。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:18)

对于那些升级到Entity Framework 6的人来说,迁移初始化有一个新的重载,这使得这更容易:

    // Parameters:
    //   useSuppliedContext:
    //     If set to true the initializer is run using the connection information from the
    //     context that triggered initialization. Otherwise, the connection information
    //     will be taken from a context constructed using the default constructor or registered
    //     factory if applicable.
    public MigrateDatabaseToLatestVersion(bool useSuppliedContext);

使用此方法,您可以使用注入的DbContext运行迁移,如下所示:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, MyMigrationConfiguration>(useSuppliedContext: true));

using (var context = kernel.Get<MyDbContext>())
    context.Database.Initialize(false);

答案 1 :(得分:5)

以下是我最终使用的方法,使用this answer中的自定义IDatabaseInitializer<T>代码,这对我帮助很大。

首先,我们将另一个构造函数添加到DataStore类(DbContext),该类不需要连接字符串参数:

public class DataStore : DbContext, IDataStore
{
    public int UserID { get; private set; }

    // This is the constructor that will be called by the factory class 
    // if it is initialised without a connection string parameter
    public DataStore(int userId)
    {
        UserID = userId;
    }

    public DataStore(int userId, string connectionString) : base(connectionString)
    {
        UserID = userId;
    }

    public virtual IDbSet<User> Users { get; set; }

    // Rest of code here
}

然后我们对工厂类做同样的事情:

public class DataStoreFactory : Disposable, IDataStoreFactory
{
    private DataStore _database;
    private int _userId;
    private string _connectionString;

    // This is the constructor that will be called by the 
    // MigrationDataStoreFactory class
    public DataStoreFactory(int userId)
    {
        _userId = userId;
    }

    public DataStoreFactory(int userId, string connectionString)
    {
        _userId = userId;
        _connectionString = connectionString;
    }

    public IDataStore Get()
    {
        // If we have a connection string, construct our context with it,
        // if not, use the new constructor
        if(_connectionString != null)
            _database = new DataStore(_userId, _dateTimeServices, _connectionString);
        else
            _database = new DataStore(_userId, _dateTimeServices);

        return _database;
    }

    protected override void DisposeCore()
    {
        if (_database != null) _database.Dispose();
    }
}

这是自定义初始化代码:

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

    public MigrateDatabaseToLatestVersionWithConnectionString()
    {
        _config = new TMigrationsConfiguration();
    }

    public MigrateDatabaseToLatestVersionWithConnectionString(string connectionString)
    {
        // Set the TargetDatabase for migrations to use the supplied connection string
        _config = new TMigrationsConfiguration { 
            TargetDatabase = new DbConnectionInfo(connectionString, 
                                                  "System.Data.SqlClient")
        };
    }

    public void InitializeDatabase(TContext context)
    {
        // Update the migrator with the config containing the right connection string
        DbMigrator dbMigrator = new DbMigrator(_config);
        dbMigrator.Update();
    }
}

我们的自定义上下文工厂(仅由Code First Migrations调用)现在可以继续使用需要连接字符串的DataStore构造函数:

public class MigrationDataStoreFactory : IDbContextFactory<DataStore>
{
    public DataStore Create()
    {
        return new DataStore(0); 
    }
}

只要我们将数据库初始化程序设置为自定义初始化程序并传入连接字符串(在我的情况下在Global.asax中完成),迁移将使用正确的连接:

Database.SetInitializer<DataStore>(new MigrateDatabaseToLatestVersionWithConnectionString<DataStore, MyMigrationsConfiguration>(INJECTED_CONNECTION_STRING_HERE));

希望所有有意义的事情 - 随意在评论中要求澄清。

答案 2 :(得分:1)

首先定义数据库设置界面,例如IDBConnectionSettings。 在app.config中添加连接字符串:

  <connectionStrings>
    <add name=" ConnectionString "
      connectionString="Integrated Security=SSPI; Persist Security Info=False;   InitialCatalog=DB; Data Source=(local);"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

要从设置文件或 app.config 中检索连接字符串,您需要执行以下操作:

 public class DBConnectionSettings()
 {
   get ConnectionString
   {
       var connections = ConfigurationManager.ConnectionStrings;
       // From app.config you will get the connection string
       var connectionString = connections["ConnectionString"].ConnectionString;

     return connectionString;
   }
 }

现在您必须在使用之前在代码中的某处注册接口。

unityContainer.Register<IDBConnectionSettings>();

您可以随时随地使用它。

public class MigrationDataStoreFactory : IDbContextFactory<DataStore>
{
    public string _connectionString { get; set; }

    public MigrationDataStoreFactory(UnityContainer unityContainer)
    {
        _connectionString = unityContainer.Resolve<IDBConnectionSettings>().ConnectionString;
    }

    public DataStore Create()
    {
        return new DataStore(0, new DateTimeProvider(() => DateTime.Now), _connectionString);
    }
}

默认构造函数

的更新

创建一个静态方法或将此代码放在默认构造函数中,这样就不必给出任何参数。

  var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Application.StartupPath + Path.DirectorySeparatorChar + @"app.config" }; // application name must be

  using (var unityContainer = new UnityContainer())
  {
    var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

    unityContainer.LoadConfiguration(unitySection, "ConnectionString");
    {
     unityContainer.Resolve<IDBConnectionSettings>();
     .... 
     ....

我希望这能解决你的问题!感谢