在Azure中设置ASP.NET 5 Web应用程序的SQL连接字符串
|
我在Visual Studio 2015中创建了一个新的ASP.NET 5(MVC 6)Web应用程序。我还设置了一个Azure Web应用程序,从GitHub中拉入应用程序并运行它。这可以正常工作,但是我无法连接到Azure上的数据库。 在本地工作,它使用config.json和代码Data:DefaultConnection:ConnectionString为连接字符串。 我如何离开代码,并在Azure中工作?我尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置。并使用“SQLCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作为关键字。 (设置应用程序设置似乎没有办法,我认为我提供的价值太长)。 那么,如何将Azure数据库的连接字符串提供给我的Azure Web应用程序(ASP.NET 5),而不是在源代码控制中进行检查? 更新 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
解决方法简短答案
你很近 >转到Azure网络应用程序>配置>连接字符串。 使用timesheet_db而不是DefaultConnection的示例 这是我自己的时间表应用程序的一个例子。我的连接字符串被命名为timesheet_db。只需使用DefaultConnection替换该字符串的所有实例,以使示例适应您的用例。 Azure网络应用配置 Azure网络应用服务控制管理器 https://myWebAppName.scm.azurewebsites.net/Env的在线服务控制管理员将显示连接字符串。 Startup.cs 在启动中设置配置设置,以使环境变量覆盖config.json。 public IConfiguration Configuration { get; set; }
public Startup()
{
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables(); <----- will cascade over config.json
}
在启动中配置数据库。 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的连接字符串替换它的所有实例,一切都可以正常工作。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 高级:HttpModule Init()方法在应用程序的生命中
- asp.net中的ASHX处理程序文件的好处是什么?
- asp.net – Visual Studio 2012不发布项目
- asp.net – VS 2010失败调试:HttpException在LoadControl中
- asp.net-mvc – 通过删除空格打破我的calc语句的MVC捆绑?
- asp.net-mvc – 从桌面开发人员的角度来学习ASP.NET MVC的建
- nuget-package – MvcScaffolding是否通过命令行与VS 2013
- asp.net-mvc-3 – ActionButton而不是ActionLink
- asp.net-mvc-3 – 构造函数注入用作Action方法参数的View M
- .net – MVC – 是模型查看还是控制器查看?
- asp.net-mvc-3 – 在MVC 3视图模型上使用Editabl
- vs2008中文版提供下载(包含中文msdn),包括vs200
- asp.net – IIS重写输入类型
- asp.net-mvc – ASP.NET MVC 4移动显示模式停止工
- 如果复选框被选中,ASP.NET – 需要一个文本框
- asp.net-mvc-3 – ASP.NET MVC语言更改链接
- asp.net-mvc-3 – 在App_code文件夹中使用razor
- 基于声明的身份 – 在asp.net MVC5 EF6中使用流畅
- 在ASP.NET MVC3中实现的示例项目插件jquery文件上
- asp.net-mvc – System.Web.Optimization与Micro
