1

I know that to use EF Core in a class library a startup project is needed, as explained here: Is EF Core Add Migration Supported from .NET Standard Library? However, I've managed to use dotnet ef migrations bundle locally using a single class library project (without using a startup project) by

  1. Adding a ConfigurationBuilder you dbcontext
  2. Overriding the "OnConfiguring" method to use the ConfigurationBuilder for reading the connection string where you have it. In my case from user secrets
    public partial class MyContext : DbContext
    {
        private readonly IConfiguration configuration;

        //will take the connection string from user secrets
        public MyContext()
        {
            this.configuration = new ConfigurationBuilder()
                .AddUserSecrets<ClpQUkMiContext>()
                .Build();
        }

        public MyContext(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                var connectionString = configuration["ConnectionStringInUserSecret"]
                optionsBuilder.UseSqlServer(connectionString);
            }
        }

However, when I'm trying to use it in azure pipeline it's failing to find the connection string, which I have stored in a secret variable. So far I've tried

  1. Adding ".AddEnvironmentVariables()" to the configuration builder.
  2. Mapping the secret variable in the yaml file, like
variables:
 MyConnectionString: $(MySecreatConnectionString) # Setting variable to be read by the context

I would appreciate any help.

Juan
  • 41
  • 5

1 Answers1

2

Managed to solve the issue: To map a secret variable into an environment one that can be read by the configuration, it must be done inside each task, as crlearly explained here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables

dev variable inside the task

Juan
  • 41
  • 5