我正在使用.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; }
}
并通过以下设置将其注入我的控制器
services.AddDbContext<DashboardContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DashboardDatabase")));
现在 DashboardData
类使用 Table
Attirbute连接到正确的表和模式 .
[Table("TableName", Schema = "dbo")]
public partial class DashboardData
{
...
}
我想做的是将这两个字符串“TableName”和“dbo”提取到我的appsettings.json配置中 . 我已经将配置添加到appsettings,创建了TableConfiguration类并设置了依赖注入:
TableConfiguration.cs
public class TableConfiguration
{
public string DatabaseView { get; set; }
public string DatabaseSchema { get; set; }
}
appsettings.json
"TableConfiguration": {
"DatabaseTable": "TableName",
"DatabaseSchema": "dbo"
}
startup.cs
services.Configure<TableConfiguration>(Configuration.GetSection("TableConfiguration"));
是否可以在DasboardData属性中注入或以其他方式使用配置?
1 回答
在你的_1082694中:
然后,将
IOptions<TableConfiguration> tableConf
注入您的上下文并存储它以供OnModelCreating()
以后使用: