用户可以在Azure中5 ASP.NET Web应用程序的SQL连接字符串 [英] Setting the SQL connection string for ASP.NET 5 web app in Azure
问题描述
我已经建立在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的为重点。
块引用>您正在接近。
- 转到Azure的Web应用程序>配置>连接字符串。
- 添加一个连接字符串名为
DefaultConnection
。- 使用
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 codeData: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.
- Go to Azure web app > configure > connection strings.
- Add a connection string with the name
DefaultConnection
.- Use
Configuration.Get("Data:DefaultConnection:ConnectionString")
to access it.
Example using
timesheet_db
instead ofDefaultConnection
This is an example from my own timesheet application. My connection string was named
timesheet_db
. Just replace all instances of that string withDefaultConnection
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 namedDefaultConnection
and everything will work.这篇关于用户可以在Azure中5 ASP.NET Web应用程序的SQL连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!