【问题标题】:I cannot retrieve connection string in DbContext class in .NET Core 2.2 Razor Pages我无法在 .NET Core 2.2 Razor Pages 的 DbContext 类中检索连接字符串
【发布时间】:2019-10-04 06:58:15
【问题描述】:

在 Startup.cs 中配置服务这有效:

var connection = Configuration["ConnectionStrings:DefaultConnection"];

        services.AddDbContext<MyDbContext>(
                 options => { options.UseSqlServer(connection); });

在我的 MyDbContext.cs 类中这不起作用:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

using OESAC.Models;

namespace OESAC.Models
{
    public class MyDbContext : DbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
        { }

        public DbSet<Courses> Courses { get; set; }
        public DbSet<Sponsors> Sponsors{ get; set; }

        public IConfiguration Configuration { get; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

           var connection = Configuration["ConnectionStrings:DefaultConnection"];

            optionsBuilder.UseSqlServer(connection);

            ;
        }


    }
}

我可以对连接字符串进行硬编码,但我希望它根据我的 appSettings.Development.json 和 appSettngs.json(生产)动态更改。我不敢相信我花了这么多时间试图弄清楚这一点。它让我付出的代价远远超过了我得到的报酬。

【问题讨论】:

    标签: asp.net-core connection-string razor-pages


    【解决方案1】:

    您需要在构造函数中注入IConfiguration 才能访问配置。

    public class MyDbContext : DbContext
    {
        private readonly IConfiguration _configuration;
    
        public MyDbContext(IConfiguration configuration)       
        {
           _configuration = configuration
        }
    
        public DbSet<Courses> Courses { get; set; }
        public DbSet<Sponsors> Sponsors{ get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
    
           var connection = _configuration["ConnectionStrings:DefaultConnection"];
    
            optionsBuilder.UseSqlServer(connection);            
        }
    }
    

    Startup.cs:

    services.AddDbContext<ApplicationDbContext>();
    

    【讨论】:

    • 谢谢!哇。我在设置 MyDbContext 时还差得很远。我只是不是火箭科学家。您在 Startup.cs 中的建议也有效,现在我只有一个地方可以调用连接字符串。有趣的是,代码总是先查看 appSettings.json,然后再查看 appSettings.[Enviroment].json。第二个 appsettings 将覆盖第一个 appsettings 中的名称。因此,通过在两个文件中放置“DefaultConnection”:并将一个指向 Dev 和一个指向 Prod,appsettings.Development.json 将在开发环境中工作,并且在发布时将其省略,因此使用生产“DefaultConnection”。
    • @JustJohn:您不应该在 appsettings 中存储连接字符串等敏感信息,因为它很容易意外提交,并且需要提交其他非敏感值(用于部署应用程序时)。您应该使用 user secrets 代替开发和环境变量(docker)或类似 azure key-value configuration provider 的东西用于 azure
    • @Tseng:谢谢,好主意。目前该应用程序处于原始 Beta 模式,我只是想让它工作,以便客户可以看到它。该数据是虚拟数据。我想我会在院子里安装警报系统或看门狗之前建造房子。
    • @JustJohn:意外提交的问题是,一旦提交,敏感数据就很难从存储库中删除,特别是在像 git 这样的分布式版本系统上,其中多人拥有本地副本。但是,即使从主 git 存储库中删除它也会破坏很多人的东西,因为每个打开的拉取请求和分支都会被破坏,这些分支是基于意外提交后的提交(从 gi​​t 中删除东西意味着重写整个 git 历史和使用新的提交哈希创建)
    • @Tseng:没有 Git。
    猜你喜欢
    • 2019-10-03
    • 2021-11-18
    • 2016-12-17
    • 1970-01-01
    • 2019-10-12
    • 2019-08-22
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多