ASP.NET 5 / EF7 connection string in web.config?

In ASP.Net 4.5, I could put the connection string in web.configto take advantage web.config transformationsso that I can work locally with the development database, and then when I publish this, I will point to the production database. Now I am working with ASP.Net 5 and EF 7, which seem to use a file config.jsonto store connection strings instead web.config. With this new way of storing files, I can't figure out how to make something like web.config transformationsfrom the past. How can I either configure config.jsonfor this or configure it so that I can do this in the web.config file and look at EF for lines?

+4
source share
2 answers

The conversion syntax is web.configoriented to the XML data format. New configurations consist of some JSON files, and it is very easy to implement creation scripts.

First of all, ASP.NET support allows you to set the target environment using an environment variable ASPNET_ENVor by installing Hosting:Environmentto a launchSettings.jsonfile (see Propertiesyour project folder ). The file launchSettings.jsoncan be changed in Visual Studio in the project properties. First you need to select "Profile"

enter image description here

and make a setting for each profile. Alternatively, you can simply edit the file Properties\launchSettings.jsonmanually.

, hosting.json, . , , , , server.urls hosting.json hosting.Development.json .

appsettings.json, Startup Startup.cs. :

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 MyContext SM9Context. , appsettings.json appsettings.Development.json, ( Data:ConnectionString Data:SM9ConnectionString):

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

ASP.NET appsettings.json appsettings.Development.json, .

, ASP.NET 5.

+3

- , *.json, ;

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

ASPNET.

, , , .

docs .

+2

Source: https://habr.com/ru/post/1620918/


All Articles