如何解决:尽管在构建管道中设置了变量,但在Azure Devops中连接字符串为null [英] How to fix: In Azure Devops connection string is null despite setting Variable in the build Pipeline

查看:69
本文介绍了如何解决:尽管在构建管道中设置了变量,但在Azure Devops中连接字符串为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Azure Devops中的构建管道有问题,来自构建管道的变量不能替换appsettings.json中的空配置.下面有更多详细信息.

I have problem with build pipeline in Azure Devops, with variables from build pipeline not replacing empty configuration in appsettings.json. Below there is more details.

我当前的测试项目是使用asp.net核心技术构建的,并已连接到SQL Server.我还使用Entity Framework Core和autofac.

My current test project is build using asp.net core technology and is connected to SQL server. I also use Entity Framework Core and autofac.

要连接到SQL Server,请使用appsettings.json配置:

To connect to SQL server I use appsettings.json configuration:

{
  "ConnectionStrings": {
    "AzureDbConnectionString": ""
  }
}

但是我的凭据存储在secrets.json中

but my credentials are stored in secrets.json

{
  "ConnectionStrings": {
    "AzureDbConnectionString": "Server=tcp:servername-db-srv.database.windows.net,1433;Initial Catalog=dbname-db;Persist Security Info=False;User ID=user;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  }
}

我已经配置了构建管道变量:名称:

I've configured my build pipeline variable: Name:

ConnectionStrings--AzureDbConnectionString 

值:

Server=tcp:servername-db-srv.database.windows.net,1433;Initial Catalog=dbname-db;Persist Security Info=False;User ID=user;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

在构建管道中运行生成迁移脚本"时会发生问题.

The problem occurs while running Generate Migration Scripts in the build pipeline.

Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Microsoft.EntityFrameworkCore.DbContextOptions[] -> λ:Microsoft.EntityFrameworkCore.DbContextOptions -> λ:Microsoft.EntityFrameworkCore.DbContextOptions`1[[AspNetAutofacAzure02.Data.SchoolContext, AspNetAutofacAzure02, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. ---> System.ArgumentException: The string argument 'connectionString' cannot be empty.
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)

就像我提到的那样,似乎在生成脚本时未使用该变量.

Like I mentioned, it looks like the variable isn't used while generating the script.

我在这里做错什么了吗?

Is there anything wrong I do here?

推荐答案

您是否尝试了 ConnectionStrings:AzureDbConnectionString ?这是覆盖appsettings.json的正常格式.或 ConnectionStrings__AzureDbConnectionString .

Did you try ConnectionStrings:AzureDbConnectionString? That's the normal format for overriding appsettings.json. Or ConnectionStrings__AzureDbConnectionString.

如果该值来自密钥库,格式为 ConnectionStrings--AzureDbConnectionString ,则只需映射一个新变量: ConnectionStrings:AzureDbConnectionString = $(ConnectionStrings--AzureDbConnectionString)

If the value is coming from keyvault in the format ConnectionStrings--AzureDbConnectionString, then just map a new variable: ConnectionStrings:AzureDbConnectionString = $(ConnectionStrings--AzureDbConnectionString)

这篇关于如何解决:尽管在构建管道中设置了变量,但在Azure Devops中连接字符串为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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