Entity framework 构造函数注入IDesignTimeDbContextFactory

Entity framework 构造函数注入IDesignTimeDbContextFactory,entity-framework,entity-framework-core,Entity Framework,Entity Framework Core,我正在尝试在.NET标准2.0类库中使用迁移EFCore2.0,到目前为止,我有类似的 public class SomeContextFactory : IDesignTimeDbContextFactory<SomeContext> { private Configuration _configuration; public SomeContextFactory(Configuration configuration) { _config

我正在尝试在.NET标准2.0类库中使用迁移EFCore2.0,到目前为止,我有类似的

public class SomeContextFactory : IDesignTimeDbContextFactory<SomeContext>
{
    private Configuration _configuration;

    public SomeContextFactory(Configuration configuration)
    {
        _configuration = configuration;
    }

    public SomeContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<SomeContext>();

        optionsBuilder.UseSqlServer(_configuration.ConnectionString);

        return new SomeContext(optionsBuilder.Options);
    }
}

public class SomeContext : DbContext
{
    public DbSet<SomeDbModel> Some { get; set; }

    public SomeContext(DbContextOptions<SomeContext> options) : base(options)
    {
    }
}
公共类SomeContextFactory:IDesignTimeDbContextFactory
{
专用配置(u配置),;
公共SomeContextFactory(配置)
{
_配置=配置;
}
公共SomeContext CreateDbContext(字符串[]args)
{
var optionsBuilder=new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(_configuration.ConnectionString);
返回新的SomeContext(optionsBuilder.Options);
}
}
公共类SomeContext:DbContext
{
公共DbSet Some{get;set;}
公共SomeContext(DbContextOptions):基本(选项)
{
}
}
关键是连接字符串根据环境(dev、test、prod)的不同而不同,迁移应该在
配置指定的数据库上执行

如何指示迁移将
配置
注入Startup.cs中的
SomeContextFactory

services.AddTransient<SomeContextFactory>();
services.AddTransient();
您的工厂:

public class SomeContextFactory : 
IDesignTimeDbContextFactory<SomeContext>
{
    private readonly IHostingEnvironment environment;
    private readonly IConfigurationRoot config;

    public SomeContextFactory(IConfigurationRoot config, 
IHostingEnvironment environment)
    {
        this.environment = environment;
        this.config = config;
    }

    public SomeContext CreateDbContext()
    {
        return CreateDbContext(null);
    }

    public SomeContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<SomeContext>();
        var connectionString = config.GetConnectionString("Default");

        builder.UseSqlServer(connectionString);

        return new SomeContext(builder.Options);
    }
}
公共类SomeContextFactory:
IDesignTimeDbContextFactory
{
私有只读IHostingEnvironment环境;
私有只读IConfigurationRoot配置;
公共SomeContextFactory(IConfigurationRoot配置,
IHostingEnvironment(环境)
{
这个。环境=环境;
this.config=config;
}
public SomeContext CreateDbContext()
{
返回CreateDbContext(null);
}
公共SomeContext CreateDbContext(字符串[]args)
{
var builder=new DbContextOptionsBuilder();
var connectionString=config.GetConnectionString(“默认”);
使用SQLServer(connectionString);
返回新的SomeContext(builder.Options);
}
}
IDesignTimeDbContextFactory
不支持使用DI。 实现这一目标的一种方法:

公共类AppDbContextFactory:IDesignTimeDbContextFactory
{
公共AppDbContext CreateDbContext(参数字符串[]args)
{
var options=new DbContextOptionsBuilder();
var config=GetAppConfiguration();
使用SQLServer(config.GetConnectionString(“DesignTimeAppDbConnection”);
返回新的AppDbContext(options.options);
}
IConfiguration GetAppConfiguration()
{
变量环境名称=
Environment.GetEnvironmentVariable(
“ASPNETCORE_环境”);
var dir=Directory.GetParent(AppContext.BaseDirectory);
如果(EnvironmentName.Development.Equals)(EnvironmentName,
StringComparison.Ordinalingorecase)
{                  
var深度=0;
做
dir=dir.Parent;
而(++深度<5&&dir.Name!=“bin”);
dir=dir.Parent;
}
var path=dir.FullName;
var builder=new ConfigurationBuilder()
.SetBasePath(路径)
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{environmentName}.json”,true)
.AddenEnvironmentVariables();
返回builder.Build();
}
}

不为我工作。我得到一个“没有为此对象定义无参数构造函数”。运行代码迁移的“更新数据库”命令时出错
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
  public AppDbContext CreateDbContext(params string[] args)
  {
    var options = new DbContextOptionsBuilder<AppDbContext>();
    var config = GetAppConfiguration();
    options.UseSqlServer(config.GetConnectionString("DesignTimeAppDbConnection"));

    return new AppDbContext(options.Options);
  }

  IConfiguration GetAppConfiguration()
  {
    var environmentName =
              Environment.GetEnvironmentVariable(
                  "ASPNETCORE_ENVIRONMENT");

    var dir = Directory.GetParent(AppContext.BaseDirectory);      

    if(EnvironmentName.Development.Equals(environmentName, 
        StringComparison.OrdinalIgnoreCase))
    {                  
      var depth = 0;
      do
        dir = dir.Parent;
      while (++depth < 5 && dir.Name != "bin");
      dir = dir.Parent;
    }

    var path = dir.FullName;

    var builder = new ConfigurationBuilder()
            .SetBasePath(path)
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{environmentName}.json", true)
            .AddEnvironmentVariables();

    return builder.Build();
  }
}