[英]Executing asp.net core EF migration in a class library without hardcoding connection string
I would like to execute migration in separate environments. 我想在单独的环境中执行迁移。 I'm working with Entity Framework and ASP.NET Core. 我正在使用实体框架和ASP.NET Core。 For now I have this : 现在我有这个:
public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext Create(DbContextFactoryOptions options)
{
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer("Data Source=.\\LOCALHOST;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=true");
return new ApplicationDbContext(builder.Options);
}
}
This code is in a DAL class library who separate the database interactions from the server code. 该代码位于DAL类库中,该库将数据库交互与服务器代码分开。
It's ok to use this when executing migrations but I don't really like doing it like this. 在执行迁移时可以使用此功能,但我真的不喜欢这样做。
The problem here is that I use a local database for development but we also have a development server and a staging server with different connection strings. 这里的问题是,我使用本地数据库进行开发,但是我们也有带有不同连接字符串的开发服务器和登台服务器。 What I can't manage to do is to find a way to not hardcode the connection strings in that class. 我无法做的是找到一种不对此类中的连接字符串进行硬编码的方法。
Is there a way to configure the connection strings for different environments and indicate which to use when executing the migrations ? 有没有一种方法可以为不同的环境配置连接字符串,并指示执行迁移时使用哪个?
Yes, you can use environment variables for this. 是的,您可以为此使用环境变量。
Thats why you have appsettings.<EnvironmentName>.json
这就是为什么要进行appsettings.<EnvironmentName>.json
Configuration in ASP.NET Core 在ASP.NET Core中进行配置
Not bad Microsoft article abount Working with multiple environments 不错的Microsoft文章擅长使用多种环境
FYI FYI
Environment.GetEnvironmentVariable Environment.GetEnvironmentVariable
You need to inject DbContextOptionsBuilder<ApplicationDbContext>
into your factory constructor and then configure the DbContext in the main application's Startup.cs
您需要将DbContextOptionsBuilder<ApplicationDbContext>
注入到工厂构造函数中,然后在主应用程序的Startup.cs
配置DbContext。
public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
private readonly DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder;
public TemporaryDbContextFactory(DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder)
{
if(optionsBuilder==null)
throw new ArgumentNullException(nameof(optionsBuilder));
this.optionsBuilder = optionsBuiler;
}
public ApplicationDbContext Create(DbContextFactoryOptions options)
{
return new ApplicationDbContext(optionsBuilder.Options);
}
}
And in Startup.cs: 在Startup.cs中:
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"));
});
Configuration.GetConnectionString("MyConnectionString")
is shorthand for Configuration["ConnectionStrings:MyConnectionString"]
. Configuration.GetConnectionString("MyConnectionString")
是Configuration["ConnectionStrings:MyConnectionString"]
简写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.