Entity framework core 如何传递CreateDbContext参数?
我有一个多DBContext和DesignTimeDbContextFactory实现,如下所示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()
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”