相关疑难解决方法(0)

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

我正在使用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
}
Run Code Online (Sandbox Code Playgroud)

还有一个创建类实例的工厂DataStore类:

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

    public DataStoreFactory(int userId, string connectionString)
    {
        _userId = userId;
        _connectionString = …
Run Code Online (Sandbox Code Playgroud)

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

18
推荐指数
2
解决办法
1万
查看次数

手动将DbContext提供给DbMigrator

Platform .NET 4.5和Entity Framework 6.

问题 我有以下代码来执行迁移:

//The following function just returns an object of the Configuration() class 
//generated by code migrations
var migratorConfig = currentMigrationProvider.CreateDbMigrationConfiguration(); 
var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(migratorConfig);
dbMigrator.Update();
Run Code Online (Sandbox Code Playgroud)

问题是Update()函数尝试创建我的DbContext类的实例,出于一些好的原因,我需要手动创建上下文并将其提供给dbMigrator.那可能吗?怎么样?

c# entity-framework dbcontext ef-migrations dbmigrator

5
推荐指数
1
解决办法
2508
查看次数