asp.net core 1 appsettings.production.json不更新连接字符串 [英] asp.net core 1 appsettings.production.json not updating connection strings

查看:61
本文介绍了asp.net core 1 appsettings.production.json不更新连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在生产中使用了运行在IIS上的.net核心RC1的应用程序.我重新编写了该应用程序,因为该公司希望在.net core1上使用它,因为这是正式版本.

I had an application in production using .net core RC1 running on IIS. I re-wrote the application because the company wanted it on .net core1 since that is the official release.

在我的.net核心RC1应用程序中,我具有config.json和config.production.json,在生产版本中,我只包含了我想覆盖的设置(如连接字符串).很好.

In my .net core RC1 application I had config.json and config.production.json and in the production version I simply included the settings I wanted overridden like the connection string. That worked fine.

在.net core1中,我有appsettings.json和appsettings.production.json.但是,连接字符串似乎没有更新.似乎使用了原始appsettings.json中的localdb.

In .net core1 I have appsettings.json and appsettings.production.json. However, the connection string doesn't seem to be getting updated. It seems to use the localdb from the original appsettings.json.

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-DecisionEff-b32ae861-2422-4f88-839c-2a6d599fee45;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

appsettings.production.json:

{
    "ConnectionStrings": {
      "DefaultConnection": "Server=mydb.server;Database=DbName;User ID=User;Password=password;Trusted_Connection=False; Connection Timeout= 30;"
    }
}

在我的启动文件中,我确实将其设置为查找生产:

In my startup file I do have it set to look for production:

var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

以上内容是根据我在RC1中所做的工作建模而成的.是否还有其他更改使连接字符串没有覆盖?如果我将生产数据库连接字符串放在appsettings.json文件中,则该应用程序可以在我们的生产服务器上正常运行.因此,它不是从appsettings.production.json文件中提取设置.我知道它正在正确读取环境,因为我正在打印它,并且符合预期.

The above was modeled after what I was doing in RC1 that worked. Was there something else that changed so connections strings are not udpated? If I put the production db connection string in the appsettings.json file then the application works fine on our production server. So it is not pulling the setting from the appsettings.production.json file. I know it is reading the environment properly because I am printing it out and it is as expected.

如何在.net core 1 Web应用程序中提供不同的开发和生产数据库设置?

How do you provide different development and production db settings in .net core 1 web app?

任何帮助将不胜感激.我显然忽略了一些东西.

Any help would be appreciated. I am obviously overlooking something.

更新:在本地计算机上将ASPNETCORE_ENVIRONMENT设置为Production时,它使用正确的连接字符串.因此,Web应用程序可以在生产环境中看到合适的环境,并且可以使用连接字符串.在构建过程中是否需要配置某些内容以发送不同的环境设置?还是有一种方法可以为环境打印.json文件中的不同设置?

UPDATE: When I run this on my local machine setting ASPNETCORE_ENVIRONMENT to Production it uses the proper connection string. So the web app is seeing the proper environment on production and can use the connection string. Is there something that has to be configured during build to send the different environment settings? Or is there a way it can print the different settings from the .json files for the environments?

更新2:我注意到,当我发布应用程序时,只有appsettings.json仅具有localdb连接.似乎没有生成任何appsettings.production.json文件.现在应该在构建过程中指定一些地方,这些不同的设置应该会出现吗?

UPDATE 2: I noticed that when I publish the application the there is only appsettings.json which has the localdb connection only. There doesn't seem to be any appsettings.production.json file generated. Is there somewhere you have to specify during the build now that these different settings should come across?

推荐答案

您可能忘记设置环境变量了.您必须将 ASPNETCORE_ENVIRONMENT 设置为 Production

You probably forgot to set the environment variable that sets the environment. You have to set ASPNETCORE_ENVIRONMENT to Production

这篇关于asp.net core 1 appsettings.production.json不更新连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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