用户可以在Azure中5 ASP.NET Web应用程序的SQL连接字符串 [英] Setting the SQL connection string for ASP.NET 5 web app in Azure

查看:164
本文介绍了用户可以在Azure中5 ASP.NET Web应用程序的SQL连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立在Visual Studio 2015年一个新的ASP.NET 5(MVC 6)网络应用程序,我还设置了Azure的Web应用程序在从GitHub应用拉并运行它。这工作得很好,但我有连接到在Azure数据库的麻烦。

在当地,这个作品,它使用 config.json 和code 数据:DefaultConnection:ConnectionString的连接​​字符串。

我怎么能离开code,因为它是,并且有它在Azure中的工作吗?我试着在门户网站设置应用程序设置,无论是连接字符串和应用程序设置。而同时使用SQLCONNSTR_DefaultConnection和数据:DefaultConnection:ConnectionString的为重点。

(设置应用程序设置似乎并没有被工作方式。我觉得我提供的价值是太长)。

所以,我怎么能为我的Azure数据库的Azure我的web应用程序(ASP.NET 5)连接字符串,没有在源代码控制,检查它?

更新
我Startup.cs看起来像这样(见<一href=\"https://github.com/petermorlion/RedStar.Invoicing/blob/master/src/RedStar.Invoicing/Startup.cs\">the在GitHub上完整的文件):

 公共启动(IHostingEnvironment ENV)
{
    无功配置=新配置()
        .AddJsonFile(config.json)
        .AddJsonFile($配置{} env.EnvironmentName以.json,可选的:真正的);    如果(env.IsEnvironment(发展))
    {
        configuration.AddUserSecrets();
    }    configuration.AddEnvironmentVariables();
    配置=配置;
}

ConfigureServices 方法,还有:

  services.Configure&LT;&AppSettings的GT;(Configuration.GetSubKey(的AppSettings));

和,也在 ConfigureServices 方法:

  services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext&LT; ApplicationDbContext&GT;(选项=&GT;
                   options.UseSqlServer(配置[数据:DefaultConnection:ConnectionString的]))
            .AddDbContext&LT; InvoicesDbContext&GT;(选项=&GT;
                   options.UseSqlServer(配置[数据:DefaultConnection:ConnectionString的]));
//因此,这是我希望我的Azure中的应用程序来使用连接字符串我
//门户提供


解决方案

简短的回答


  

我已经尝试设置应用程序设置的门户,无论是连接字符串和应用程序设置。而同时使用SQLCONNSTR_DefaultConnection和数据:DefaultConnection:ConnectionString的为重点。


您正在接近。


  1. 转到Azure的Web应用程序>配置>连接字符串。

  2. 添加一个连接字符串名为 DefaultConnection

  3. 使用 Configuration.Get(数据:DefaultConnection:ConnectionString的)。来访问它

使用示例 timesheet_db 而不是 DefaultConnection

这是从我自己的时间表应用程序的例子。我的连接字符串被命名为 timesheet_db 。刚刚与 DefaultConnection 替换字符串的所有实例的例子适应您的使用情况。

Azure的Web应用程序配置

Azure的Web应用程序服务控制管理器

在<一个在线服务控制管理器href=\"https://myWebAppName.scm.azurewebsites.net/Env\">https://myWebAppName.scm.azurewebsites.net/Env将显示连接字符串。

Startup.cs

启动安装程序配置设置这样的环境变量覆盖config.json。

 公共IConfiguration配置{搞定;组; }
公共启动()
{
    配置=新配置()
        .AddJsonFile(config.json)
        .AddEnvironmentVariables(); &LT; -----将级联在config.json
}

启动配置数据库

 公共无效ConfigureServices(IServiceCollection服务)
{
    服务
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext&LT; ProjectContext&GT;(选项=&GT;
        {
            VAR CONNSTRING =
                Configuration.Get(数据:timesheet_db:ConnectionString的);
            options.UseSqlServer(CONNSTRING);
        });
}

当然,示例使用名为连接字符串 timesheet_db 。对你来说,与 DefaultConnection 命名自己的连接字符串,这样就可以了取代它的所有实例。

I have created a new ASP.NET 5 (MVC 6) web application in Visual Studio 2015. I've also set up an Azure web app to pull in the app from GitHub and run it. This works fine, but I'm having trouble connecting to the database on Azure.

Locally, this works, and it uses config.json and in code Data:DefaultConnection:ConnectionString for the connection string.

How can I leave the code as it is, and have it work in Azure too? I've tried setting the Application Settings in the portal, both the Connection Strings and the App Settings. And using both "SQLCONNSTR_DefaultConnection" and "Data:DefaultConnection:ConnectionString" as the key.

(Setting the App Settings doesn't seem to work by the way. I think the value I provide is too long).

So how can I provide the connection string for my Azure database to my Azure web app (ASP.NET 5), without checking it in in source control?

Update My Startup.cs looks like this (see the full file on GitHub):

public Startup(IHostingEnvironment env)
{
    var configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

    if (env.IsEnvironment("Development"))
    {
        configuration.AddUserSecrets();
    } 

    configuration.AddEnvironmentVariables();
    Configuration = configuration;
}

In the ConfigureServices method, there is also:

services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

and, also in the ConfigureServices method:

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
            .AddDbContext<InvoicesDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// So this is where I want my app in Azure to use the connection string I
// provide in the portal

解决方案

Short answer

I've tried setting the Application Settings in the portal, both the Connection Strings and the App Settings. And using both "SQLCONNSTR_DefaultConnection" and "Data:DefaultConnection:ConnectionString" as the key.

You're close.

  1. Go to Azure web app > configure > connection strings.
  2. Add a connection string with the name DefaultConnection.
  3. Use Configuration.Get("Data:DefaultConnection:ConnectionString") to access it.

Example using timesheet_db instead of DefaultConnection

This is an example from my own timesheet application. My connection string was named timesheet_db. Just replace all instances of that string with DefaultConnection to adapt the example to your use case.

Azure web app configuration

Azure web app service control manager

The online service control manager at https://myWebAppName.scm.azurewebsites.net/Env will show the connection strings.

Startup.cs

Setup configuration settings in Startup so that the environmental variables overwrite the config.json.

public IConfiguration Configuration { get; set; }
public Startup()
{
    Configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();    <----- will cascade over config.json
}

Configure the database in Startup.

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ProjectContext>(options =>
        {
            var connString =
                Configuration.Get("Data:timesheet_db:ConnectionString");
            options.UseSqlServer(connString);
        });
}

Of course, the example uses a connection string named timesheet_db. For you, replace all instances of it with your own connection string named DefaultConnection and everything will work.

这篇关于用户可以在Azure中5 ASP.NET Web应用程序的SQL连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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