在Azure中为ASP.NET Core Web应用设置SQL连接字符串 [英] Setting the SQL connection string for ASP.NET Core web app in Azure
问题描述
我已经在Visual Studio 2015中创建了一个新的ASP.NET Core Web应用程序.我还设置了一个Azure Web应用程序,以从GitHub中提取该应用程序并运行它.效果很好,但是我在连接到Azure上的数据库时遇到了麻烦.
I have created a new ASP.NET Core 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.
这在本地有效,并且它使用config.json
并在代码Data:DefaultConnection:ConnectionString
中使用了连接字符串.
Locally, this works, and it uses config.json
and in code Data:DefaultConnection:ConnectionString
for the connection string.
如何保持代码不变,并使其在Azure中也能正常工作?我尝试在门户中设置应用程序设置",包括连接字符串"和应用程序设置".并同时使用"SQLCONNSTR_DefaultConnection"和"Data:DefaultConnection:ConnectionString"作为键.
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).
那么,如何在不将其在源代码管理中检入的情况下,将我的Azure数据库的连接字符串提供给我的Azure Web应用程序(ASP.NET 5)?
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?
更新 我的Startup.cs看起来像这样(请参阅完整的GitHub上的文件):
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;
}
在ConfigureServices
方法中,还有:
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
,也可以在ConfigureServices
方法中:
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
推荐答案
简短答案
我尝试在门户中设置应用程序设置",包括连接字符串"和应用程序设置".并同时使用"SQLCONNSTR_DefaultConnection"和"Data:DefaultConnection:ConnectionString"作为键.
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.
您已经关闭.
- 转到Azure Web应用>配置>连接字符串.
- 添加名称为
DefaultConnection
的连接字符串. - 使用
Configuration.Get("Data:DefaultConnection:ConnectionString")
进行访问.
- 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.
使用timesheet_db
代替DefaultConnection
的示例
这是我自己的时间表应用程序中的一个示例.我的连接字符串名为timesheet_db
.只需用DefaultConnection
替换该字符串的所有实例,以使示例适合您的用例.
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.
位于 https://myWebAppName.scm.azurewebsites.net/Env将显示连接字符串.
The online service control manager at https://myWebAppName.scm.azurewebsites.net/Env will show the connection strings.
在Startup
中设置配置设置,以便环境变量覆盖config.json.
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
}
在Startup
中配置数据库.
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ProjectContext>(options =>
{
var connString =
Configuration.Get("Data:timesheet_db:ConnectionString");
options.UseSqlServer(connString);
});
}
当然,该示例使用名为timesheet_db
的连接字符串.对您来说,用您自己的名为DefaultConnection
的连接字符串替换它的所有实例,一切都会正常工作.
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中为ASP.NET Core Web应用设置SQL连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!