Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions environment variable is always null

In my V3 functions project I am trying to wire up Entity Framework and pass a connection string from an environment variable.

In my local.settings.json file I have:

{
  "Values" : {
    "SqlConnectionString" : "<localdb connection string here>"
  }
}

I am wiring up the context using IDesignTimeContextFactory as per this blog post:

public class ContextFactory : IDesignTimeDbContextFactory<Context>
    {
        public Context CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<Context>();
            var connectionString = Environment.GetEnvironmentVariable("SqlConnectionString", EnvironmentVariableTarget.Process);
            optionsBuilder.UseSqlServer(connectionString);

            return new Context(optionsBuilder.Options);
        }
    }

When I try adding a migration connectionString is null.

Can anyone see what the problem is here?

Edit:

As suggested, I've also tried defining the connection string and injecting IConfiguration like this:

    public class ContextFactory : IDesignTimeDbContextFactory<Context>
    {
        private readonly IConfiguration _configuration;

        public ContextFactory(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public Context CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<Context>();
            optionsBuilder.UseSqlServer(_configuration.GetConnectionString("SqlConnectionString"));

            return new Context(optionsBuilder.Options);
        }
    }

but I get the following exception:

System.MissingMethodException: No parameterless constructor defined for type 'EbayTools.Functions.Domain.ContextFactory'.

like image 831
NRKirby Avatar asked Jul 03 '26 07:07

NRKirby


1 Answers

EXPLANATION

I ran into the same problem while following the same blog post/tutorial!

When running the Entity Framework Core CLI tools the values in local.settings.json are not added to the list of environment variables. You can check this by prompting the debugger to run (see code).

To access a value in the local.settings.json when running an Entity Framework Core CLI command I used the ConfigurationBuilder (again see below).

Lastly, I stored my connection string under 'Values' in the local.settings.json file, not ConnectionStrings.


SOLUTION

ProjectContextFactory.cs

public class ProjectContextFactory : IDesignTimeDbContextFactory<ProjectDbContext>
{
    public ProjectDbContext CreateDbContext(string[] args)
    {
        // Prompt debugger to open when running ef cli command.
        // Remove this when you no longer need to debug
        Debugger.Launch();

        // See that environment variables list doesn't contain your key value and therefore connectionString variable returns null
        var variables = Environment.GetEnvironmentVariables();
        var connectionString = Environment.GetEnvironmentVariable("SQLDB_CONNECTIONSTRING");

        // Setup config
        var config = new ConfigurationBuilder()
            .SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        // Get connection string from config
        var connectionString = config.GetValue<string>("Values:SQLDB_CONNECTIONSTRING");

        var optionsBuilder = new DbContextOptionsBuilder<ProjectDbContext>();
        optionsBuilder.UseSqlServer(connectionString);

        return new ProjectDbContext(optionsBuilder.Options);
    }
}

local.settings.json

{
   "IsEncrypted": false,
   "Values": { 
       "AzureWebJobsStorage": "UseDevelopmentStorage=true",
       "SQLDB_CONNECTIONSTRING": "<connectionString>
   }
}
like image 119
boon Avatar answered Jul 05 '26 21:07

boon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!