【问题标题】:Azure functions .net 5 UseSqlServer configurationAzure 函数 .net 5 UseSqlServer 配置
【发布时间】:2021-04-06 13:14:04
【问题描述】:

我正在尝试使用 .net 5“dotnet-isolated”部署一个 azure 函数,但无法在其上运行 sql server。这是我的配置启动

         static async Task Main(string[] args)
            {
    #if DEBUG
                Debugger.Launch();
    #endif
                var host = new HostBuilder()
                    .ConfigureAppConfiguration(c =>
                    {
                        c.AddCommandLine(args);
                    })
                    .ConfigureFunctionsWorkerDefaults((c, b) =>
                    {
                        b.UseFunctionExecutionMiddleware();
                    })
                    .ConfigureServices((context, services) =>
                                            {
                                                services.AddDbContext<ApplicationDbContext>(options =>
                                                    options.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")));
                                            })
                    .Build();
                
                await host.RunAsync();
            }

好像是

options.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")))

无法识别 Azure 配置

【问题讨论】:

    标签: .net azure entity-framework azure-functions .net-5


    【解决方案1】:

    问题出在 ConfigureAppConfiguration 中。必须是:

    .ConfigureAppConfiguration(c =>
                    {
                        c.AddEnvironmentVariables();
                        c.AddCommandLine(args);
                    })
    

    所以,最后的 Main 是:

     static async Task Main(string[] args)
            {
    #if DEBUG
                Debugger.Launch();
    #endif
                var host = new HostBuilder()
                    .ConfigureAppConfiguration(c =>
                    {
                        c.AddEnvironmentVariables();
                        c.AddCommandLine(args);
                    })
                    .ConfigureFunctionsWorkerDefaults((c, b) =>
                    {
                        b.UseFunctionExecutionMiddleware();
                    })
                    .ConfigureServices((context, services) =>
                                            {
                                                services.AddDbContext<ApplicationDbContext>(options =>
                                                    options.UseSqlServer(context.Configuration.GetConnectionString("DefaultConnection")));
                                            })
                    .Build();
                
                await host.RunAsync();
            }
    

    【讨论】:

      【解决方案2】:

      似乎 azure 没有与您的构建连接,要么是因为您对其进行了不稳定的配置,要么是因为您希望它如此快速地处理过多的 sql 数据。我猜是第二个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-16
        • 1970-01-01
        • 2021-10-24
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多