如何在另一个项目的上下文中读取appsettings.json? Asp.Net核心 [英] How to read appsettings.json in context in another project? Asp.Net Core
问题描述
我需要在两个地方使用相同的ConnectionString
.在我的Web项目Insig.Api
中,该项目包含appsettings.json
中的ConnectionString
,并且在另一个项目类库Insing.Infrastructure
中,这是我的数据库上下文.
I need to use the same ConnectionString
in two places. In my web project Insig.Api
which contains ConnectionString
from appsettings.json
and in another project class library Insing.Infrastructure
where is my db context.
Insig.Api-Startup.cs
Insig.Api - Startup.cs
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton(Configuration);
services.AddDbContext<InsigContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
}
Insig.Infrastructure-InsigContext.cs
Insig.Infrastructure - InsigContext.cs
public class InsigContext : DbContext, IDesignTimeDbContextFactory<InsigContext>
{
public InsigContext() { }
public InsigContext(DbContextOptions<InsigContext> options) : base(options) { }
public DbSet<Sample> Samples { get; set; }
public InsigContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<InsigContext>();
builder.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=InsigDB;Integrated Security=True;MultipleActiveResultSets=True");
// Here I would like to use ConnectionString instead of raw string.
return new InsigContext(builder.Options);
}
}
如您所见,由于迁移(从代码优先"方法开始),在上下文中也需要ConnectionString
.
As you can see ConnectionString
is needed in context as well because of Migrations (from Code First approach).
编辑-以下代码无效.当我尝试Add-Migration Init
时,我收到一个错误:Value cannot be null. Parameter name: connectionString
EDIT - code below doesn't work. When I'm trying to Add-Migration Init
then I receives an error: Value cannot be null. Parameter name: connectionString
public class InsigContext : DbContext
{
private readonly string _connectionString;
public InsigContext(DbContextOptions<InsigContext> options, IConfiguration configuration) : base(options)
{
_connectionString = configuration.GetSection("ConnectionStrings:DefaultConnection").Value;
}
public InsigContext() { }
public DbSet<Sample> Samples { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(_connectionString);
}
}
}
推荐答案
我决定提供其他信息就像作者一样,我也面临着同样的问题,即将连接字符串从API项目传递给Data项目.我决定使用连接字符串指定全局设置,并将它们链接到两个项目.我通过此操作逐步完成了文章.结果,我设法在两个地方使用了连接字符串.
Like the author, I faced the same issue to pass connection string to Data project from API project. I decided to specify global settings with connection string and linked them to both projects. I went step by step through this article. In result I've managed to use connection string in two places.
这篇关于如何在另一个项目的上下文中读取appsettings.json? Asp.Net核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!