- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 .netCore 和 Entity Framework 从 SQL 数据库获取一些数据。
我已经设置了一个DbContext
public partial class DashboardContext : DbContext
{
public NotfallDashboardContext(DbContextOptions<NotfallDashboardContext> options) : base(options) {}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DashboardData>(entity =>
{
...
}
}
public virtual DbSet<DashboardData> DashboardData { get; set; }
}
并使用以下设置将其注入(inject)我的 Controller
services.AddDbContext<DashboardContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DashboardDatabase")));
现在,DashboardData
类使用 Table
Attirbute 连接到正确的表和架构。
[Table("TableName", Schema = "dbo")]
public partial class DashboardData
{
...
}
What i would like to do, is to extract these two strings "TableName" and "dbo" into my appsettings.json configuration. I already added the configuration to appsettings, made a TableConfiguration class and setup dependency injection:
表配置.cs
public class TableConfiguration
{
public string DatabaseView { get; set; }
public string DatabaseSchema { get; set; }
}
appsettings.json
"TableConfiguration": {
"DatabaseTable": "TableName",
"DatabaseSchema": "dbo"
}
启动.cs
services.Configure<TableConfiguration>(Configuration.GetSection("TableConfiguration"));
Is it possible to inject or otherwise use the configuration in the DasboardData Attribute?
最佳答案
在您的 Startup.cs
中:
services.Configure<TableConfiguration>(Configuration.GetSection("TableConfiguration"));
然后,注入(inject)IOptions<TableConfiguration> tableConf
到您的上下文中并将其存储以供您的 OnModelCreating()
稍后使用:
public class DashboardContext : DbContext
{
private readonly TableConfiguration tableConf;
public DashboardContext(DbContextOptions<DashboardContext> options, IOptions<TableConfiguration> tableConf) : base(options)
{
this.tableConf = tableConf.Value;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DashboardData>(entity =>
{
entity.ToTable(this.tableConf.DatabaseTable, this.tableConf.DatabaseSchema);
});
}
public virtual DbSet<DashboardData> DashboardData { get; set; }
}
关于c# - 使用 appsettings.json 配置 DbContext 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40744245/
我可以拥有吗?我的 app.config 中的部分包含许多设置,但也引用了 不同文件中的部分? 这将允许我保留只有开发人员应该感兴趣的配置选项,例如在主窗口上显示调试输出的选项(非常困惑但对我有用)或
我正在使用通过控制台应用程序创建的 app.config 文件,我可以使用 ConfigurationSettings.AppSettings["key1"].ToString()
我搜索了该站点,虽然我找到了一些非常有用的信息,但我无法弄清楚我的代码发生了什么。我有以下 web.config: 我
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
工具:VS2017、ASP.NET Core 2、Entity Framework Core 2、ASP.NET Core JavaScript 服务 我正在使用以下 BuildWebHost 方法:
我正在使用 ASP.NET Core 2.1。我在 appsettings.json 中有设置我使用选项模式将它们绑定(bind)到类。我想在 appsettings.Production.json
我在 appsettings.Development.json 上有点挣扎覆盖或以其他方式与 appsettings.json 合并.我不确定如何使用 appsettings.Development.
我真的很希望能够有一种方法来获取当前使用 ConfigurationManager.AppSettings [“mysettingkey”] 来获取其设置的应用程序,以使这些设置实际上来自中央数据库而
我有一些需要从多个文件访问的字符串常量。由于这些常量的值可能会不时更改,因此我决定将它们放在 AppSettings 而不是常量类中,这样我就不必在每次更改常量时都重新编译。 有时我需要处理单个字符串
我的 App.config 中有这样的代码片段 - 在 master_AppSettings.config 文件中,我将“ApplicationDSN”设置为“ABCConnectionStri
我有一个希望非常简单的问题。 如果我有值,我想从 Web.config 的 AppSettings 中找到某个键 为了简单起见,是否可以通过指定值而不是键来找到 AppSettings 条目
我正在使用 .NET Web API(4.6 框架)编写应用 我有一个我使用的属性:[ApiExplorerSettings(IgnoreApi = true)] 从我的 Swagger 中隐藏某些
在我的办公室,我们认为我们要将 Web.Config 的 AppSettings 放入数据库中。因此,我创建了以下内容,但对代码的几个方面有一些疑问。 所以我的问题是: UTILITY 类中包含“Ca
有人能帮我理解为什么在向配置文件添加值后,我不能立即将其读入应用程序吗?我做了刷新,但这不起作用。见下文: public void AddConfig(string key_value, st
我想从名为 MyAssembly.dll.config 的程序集配置文件中检索 AppSetting key 。这是配置文件的示例: 这是检索它的代码: va
我正在为 .NET 2.0 中的配置和设置类而苦苦挣扎 如果以下内容包含在名为 app.config 的文件中 我知道我可以通过以下方式访问 appSetting // this retur
我想从名为 secrets.config 的外部配置文件中读取控制台应用程序的 appSettings 的一部分,而其余部分我想从 app.config 中读取。 目前,我已经设置好了,但它似乎没有从
我有一个配置文件 app.exe.config 和 appSettings 部分有这样的东西: app.file.config 文件有这样的东西: 我需要在运行时
我目前正在从事 asp.net core (c#) Web API 项目。 对 Web API 的每次调用都将有一个唯一的 key ,以帮助识别进行调用的用户。 每个调用者都会有一组特定的配置值与之关
我是一名优秀的程序员,十分优秀!