如何使用天蓝色的连接字符串覆盖本地连接字符串 [英] how to override local connection string with azure connection string

查看:88
本文介绍了如何使用天蓝色的连接字符串覆盖本地连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.Net核心项目中使用appsettings.json作为连接字符串.我的连接字符串是:

I am using appsettings.json in .Net core project for connection string. My connection string is :

  "ConnectionStrings": {
    "OT_DB_Connection": "Data Source=108.***.**.**;Initial Catalog=O*******s;User ID=O*******s;Password=O*********$"
  },

在startup.cs中,我使用这样的键访问连接字符串

In startup.cs i am accessing connection string with key like this

   options.UseSqlServer(Configuration.GetConnectionString("OT_DB_Connection"));

我在Azure上部署了此代码,并且在Azure上拥有了sql数据库. 部署后,我的网站将如何使用azure的连接字符串? 如何在运行时使用天蓝色的连接字符串覆盖本地连接字符串.

I deployed this code on azure and i have sql database on azure. After deployment how my website will use the connection string of azure ? How to override the local connection string with azure connection string at run time.

推荐答案

您应阅读以下文章:

ASP中有多个环境配置文件. NET核心

您可以有多个appSettings,例如1用于本地环境,1用于Azure等.将应用程序发布到Azure时,可以添加一个名为ASPNETCORE_ENVIRONMENT的应用程序设置,并添加一个映射到您的环境的值,以使应用程序选择正确的配置.如果您有appSettings. Azure .json文件,则可以将ASPNETCORE_ENVIRONMENT设置为 Azure ,它将使用该配置文件.

You can have multiple appSettings e.g. 1 for you local environment and 1 for Azure etc. When you publish your app to Azure, you can add an application setting called ASPNETCORE_ENVIRONMENT and add a value that maps to your environment for your app to pick up the correct configuration. If you have an appSettings.Azure.json file you can set ASPNETCORE_ENVIRONMENT to Azure and it will use that configuration file.

如果不想采用这种方法,也可以直接在Azure中覆盖连接字符串,如下图所示.可在您的应用程序服务->应用程序设置->连接字符串下访问.您将要覆盖OT_DB_Connection.

If you do not want to take this approach, you can also override the connection string directly in Azure as show in the picture below. This is accessible under your app service -> Application Settings -> Connection Strings. You will want to override OT_DB_Connection.

这篇关于如何使用天蓝色的连接字符串覆盖本地连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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