web.config 中的 ASP.NET 5/EF7 连接字符串? [英] ASP.NET 5/EF7 connection string in web.config?

查看:15
本文介绍了web.config 中的 ASP.NET 5/EF7 连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ASP.Net 4.5 I could put my connection string in the web.config as to take advantage of web.config transformations so I could work locally with the development DB and then when I publish it would point to the production DB. I am now working with ASP.Net 5 and EF 7 which seems to use the config.json file to store connection strings instead of the web.config. With this new way of storing files I can not figure out how to do something like the web.config transformations from the past. How can I either setup config.json to do this OR configure it so I can do it in the web.config and have EF look there for the strings?

推荐答案

web.config 转换语法面向数据的 XML 格式.新配置由一些 JSON 格式的文件组成,可以非常轻松地实现暂存方案.

The web.config transformation syntax is oriented on XML format of data. The new configurations consist of some files in JSON format and one can implement staging scenarios very easy.

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

First of all ASP.NET supports allows to set destination environment by usage ASPNET_ENV environment variable or by setting Hosting:Environment in launchSettings.json file (see Properties folder of your project). The file launchSettings.json can be modified in Visual Studio in properties of the project. One should first choose the "Profile"

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

and make setting for every profile. Alternatively one can just edit the file PropertieslaunchSettings.json manually.

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

Some configuration files, like hosting.json works automatically using staging. Thus you can set for example different ports and different interface binding by specifying server.urls in hosting.json and hosting.Development.json for example.

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

To include staging logic in appsettings.json one need modify the constructor of Startup class in Startup.cs. For example:

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 文件,并创建仅覆盖 one(从两个Data:ConnectionStringData:SM9ConnectionString) 连接字符串:

The above code saves configuration in Configuration property and then uses ConfigureServices to make injection of MyContext and SM9Context database contexts. One can for example create main appsettings.json file with all productive configuration and create appsettings.Development.json file which overrides only one (from two Data:ConnectionString and Data:SM9ConnectionString) connection string:

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

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

ASP.NET will combine both files appsettings.json and optional appsettings.Development.json to create the full set of configuration parameters.

文章部分 描述了如何在ASP.NET 5.

The article and the part of documentation describes how one can use the staging in ASP.NET 5.

这篇关于web.config 中的 ASP.NET 5/EF7 连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆