Azure Functions 环境变量始终为 null

NRK*_*rby 1 c# azure entity-framework-core azure-functions

在我的 V3 函数项目中,我试图连接实体框架并从环境变量传递连接字符串。

在我的 local.settings.json 文件中,我有:

{
  "Values" : {
    "SqlConnectionString" : "<localdb connection string here>"
  }
}
Run Code Online (Sandbox Code Playgroud)

我按照这篇博文使用 IDesignTimeContextFactory 连接上下文:

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试添加迁移时connectionString为空。

任何人都可以看到这里有什么问题吗?

编辑:

按照建议,我还尝试定义连接字符串并像这样注入 IConfiguration:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我收到以下异常:

System.MissingMethodException:没有为类型“EbayTools.Functions.Domain.ContextFactory”定义无参数构造函数。

小智 5

解释

我在关注相同的博客文章/教程时遇到了同样的问题!

运行 Entity Framework Core CLI 工具时,local.settings.json 中的值不会添加到环境变量列表中。您可以通过提示调试器运行来检查这一点(请参阅代码)。

要在运行 Entity Framework Core CLI 命令时访问 local.settings.json 中的值,我使用了 ConfigurationBuilder(再次参见下文)。

最后,我将连接字符串存储在 local.settings.json 文件中的“Values”下,而不是 ConnectionStrings。


解决方案

项目上下文工厂.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

本地设置.json

{
   "IsEncrypted": false,
   "Values": { 
       "AzureWebJobsStorage": "UseDevelopmentStorage=true",
       "SQLDB_CONNECTIONSTRING": "<connectionString>
   }
}
Run Code Online (Sandbox Code Playgroud)