【问题标题】:ASP.NET 5/EF7 connection string in web.config?web.config 中的 ASP.NET 5/EF7 连接字符串?
【发布时间】:2016-03-26 08:14:30
【问题描述】:

在 ASP.Net 4.5 中,我可以将连接字符串放在 web.config 中以利用 web.config transformations,这样我就可以在本地使用开发数据库,​​然后当我发布它时,它会指向生产数据库。我现在正在使用 ASP.Net 5 和 EF 7,它们似乎使用 config.json 文件而不是 web.config 来存储连接字符串。使用这种存储文件的新方法,我无法弄清楚如何执行过去的 web.config transformations 之类的操作。如何设置 config.json 来执行此操作或配置它以便我可以在 web.config 中执行此操作并让 EF 在那里查找字符串?

【问题讨论】:

    标签: asp.net-core entity-framework-core


    【解决方案1】:

    web.config 转换语法面向 XML 格式的数据。新配置包含一些 JSON 格式的文件,可以非常轻松地实现暂存场景。

    首先,ASP.NET 支持通过使用ASPNET_ENV 环境变量或在launchSettings.json 文件中设置Hosting:Environment 来设置目标环境(请参阅项目的Properties 文件夹)。 launchSettings.json 文件可以在 Visual Studio 的项目属性中进行修改。应该首先选择“个人资料”

    并为每个配置文件进行设置。或者,您可以手动编辑文件Properties\launchSettings.json

    一些配置文件,如hosting.json 使用暂存自动运行。因此,您可以通过在hosting.jsonhosting.Development.json 中指定server.urls 来设置不同的端口和不同的接口绑定。

    要在appsettings.json 中包含暂存逻辑,需要修改Startup.csStartup 类的构造函数。例如:

    public class Startup
    {
        public static IConfigurationRoot Configuration { get; set; }
    
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
    
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc()
                .AddJsonOptions(options => {
                    options.SerializerSettings.ContractResolver =
                        new CamelCasePropertyNamesContractResolver();
                });
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<MyContext>(options => {
                    options.UseSqlServer(Configuration["Data:ConnectionString"]);
                })
                .AddDbContext<SM9Context>(options => {
                    options.UseSqlServer(Configuration["Data:SM9ConnectionString"]);
                });
        }
    }
    

    上述代码将配置保存在Configuration属性中,然后使用ConfigureServices注入MyContextSM9Context数据库上下文。例如,可以使用所有生产配置创建主 appsettings.json 文件并创建仅覆盖 一个(来自两个 Data:ConnectionStringData:SM9ConnectionString)连接字符串的 appsettings.Development.json 文件:

    {
      "Data": {
        "ConnectionString": "Server=..."
      }
    }
    

    ASP.NET 将结合两个文件 appsettings.json 和可选的 appsettings.Development.json 来创建完整的配置参数集。

    The articlethe part 的文档描述了如何在 ASP.NET 5 中使用暂存功能。

    【讨论】:

    • 您将如何从应用程序的其他位置访问公共 Configuration 属性,例如在控制器或过滤器中?我宁愿将它设为private,并通过 DI 容器提供给任何需要它的东西:services.AddSingleton&lt;IConfiguration&gt;(Configuration)
    • @ProfK:上面的代码使用staticset 属性(可以是private set;)为Configurationpublic static IConfigurationRoot Configuration { get; private set; }。因此,可以在代码的每一部分使用Startup.Configuration["Data:ConnectionString"],从应用程序配置中读取任何信息。
    【解决方案2】:

    你可以做这样的事情来从不同的 *.json 文件中提取配置,以下是基于环境或环境变量的配置;

    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
        .AddEnvironmentVariables();
    

    您还可以使用 ASPNET 机密管理器。

    每次访问配置时,它都会覆盖它拥有的任何配置值的设置,这意味着该变量将反映它最后设置的任何内容。

    查看docs on Configuration

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多