gpt4 book ai didi

c# - 如何从 Asp.Net Core 中的 DBContext 文件中的 appsettings.json 读取连接字符串?

转载 作者:行者123 更新时间:2023-12-01 21:50:37 26 4
gpt4 key购买 nike

<分区>

我在 appsettings.json 文件中定义了一个连接字符串。现在我想在我的 TextDBConext 文件中读取它,我该怎么做?

public class TestContext: DbContext
{
public DbSet<TestTable> TestTable { get; set; }
public TestContext()
{
}
public IConfiguration Configuration { get; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Connection string"); //I have hardcoded here, but I want from appsettings.json
}
}

Appsettings.json 文件:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"connection_string": "Connection String"
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com