Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core 如何传递CreateDbContext参数?_Entity Framework Core - Fatal编程技术网

Entity framework core 如何传递CreateDbContext参数?

Entity framework core 如何传递CreateDbContext参数?,entity-framework-core,Entity Framework Core,我有一个多DBContext和DesignTimeDbContextFactory实现,如下所示 public class MasterDbContextFactory : IDesignTimeDbContextFactory<MasterDbContext> { public MasterDbContext CreateDbContext(string[] args) { StringBuilder sb = new StringBuilder()

我有一个多DBContext和DesignTimeDbContextFactory实现,如下所示

public class MasterDbContextFactory : IDesignTimeDbContextFactory<MasterDbContext>
{
    public MasterDbContext CreateDbContext(string[] args)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var arg in args)
            sb.AppendLine(arg);
        System.IO.File.WriteAllText("D:\\debug.txt", sb.ToString());
 
        string server = args[1];
        string databaseName = args[2];
 
        string connectionString = string.Format(Constant.Connection, server, databaseName);
        var optionsBuilder = new DbContextOptionsBuilder<MasterDbContext>();
        optionsBuilder.UseSqlServer("connection string");
        return new MasterDbContext(optionsBuilder.Options);
    }
}

但我看不到所有参数。

该功能仅在最新版本EF Core 5.0 Preview 6中可用

参数现在从命令行流入
IDesignTimeDbContextFactory
CreateDbContext
方法。例如,要指示这是一个
dev
构建,可以在命令行上传递一个自定义参数(例如
dev
):

dotnet ef migrations add two --verbose --dev
这个版本很好用

添加带有其他参数的迁移:

Add-Migration InitialCreate -Context MasterDbContext -OutputDir Migrations\Master -Args 'P1 P2'
Update-Database -Context MasterDbContext -Args 'P1 P2'
使用其他参数更新迁移:

Add-Migration InitialCreate -Context MasterDbContext -OutputDir Migrations\Master -Args 'P1 P2'
Update-Database -Context MasterDbContext -Args 'P1 P2'

这是我安装的5.0.0-preview.6.20312.4版本。您是否可以通过
Get Module-Name EntityFrameworkCore
dotnet EF--version
确认EF Core工具的版本,当我指定时,它可以运行。更新数据库-Migration migrationName-Context MasterDbContext-appags“P1”“P2”