Azure Functions .NET Core 中的 EF Core 2.0 连接字符串 [英] EF Core 2.0 connection string in Azure Functions .NET Core

查看:23
本文介绍了Azure Functions .NET Core 中的 EF Core 2.0 连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Azure Functions 中使用 EF 核心 2.0,使用 .net 核心.我正在尝试从 local.settings.json 中读取 db ConnectionString,它的定义是:

I'm using EF core 2.0 in Azure Functions using .net core. I'm trying to read db ConnectionString from local.settings.json, which is defined:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "MyDbConnStr": "Data Source=.;Initial Catalog=xxxxxxx"
  }
}

Environment.GetEnvironmentVariable() 不返回任何连接字符串信息,我也不能将 ConfigurationManager.ConnectionStrings 与 .net core 一起使用.如何从代码中访问连接字符串?

Environment.GetEnvironmentVariable() doesn't return any connection string info neither I can use ConfigurationManager.ConnectionStrings with .net core. How do I access the connection string from the code?

推荐答案

您可以使用 Microsoft.Azure.WebJobs.Host 中的帮助程序类之一:

You could use one of the helper classes in Microsoft.Azure.WebJobs.Host:

AmbientConnectionStringProvider.Instance.GetConnectionString("MyDbConnStr")

这个类将工作委托给名为ConfigurationUtility的内部类,它做一些符合

This class is delegating the work to internal class called ConfigurationUtility, which does something in line with

var configuration = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .AddJsonFile("appsettings.json", true)
    .Build();
var conn = configuration.GetConnectionString("MyDbConnStr");

这篇关于Azure Functions .NET Core 中的 EF Core 2.0 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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