【问题标题】:Entity Framework not detecting the DB Connection string for an Azure Function实体框架未检测到 Azure 函数的数据库连接字符串
【发布时间】:2018-07-24 23:08:57
【问题描述】:

使用通过 EF 连接到数据库的控制台应用程序,我能够调用 Update-Database 并让它连接到数据库。但是,我在尝试将其迁移到 Azure 函数时遇到了麻烦。 here 提出了类似的问题,我怀疑解决方案可能是相同的,尽管没有太多细节说明可能是什么。

在我的控制台应用程序中,提供了以下相关配置:

<configuration>
  <configSections>    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <connectionStrings>
      <add name="DBConnection" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyDB;User Id= . . ." />
    </connectionStrings>
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
    </entityFramework>

在我的 local.settings.json 中,我只有数据库连接字符串:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...",
  },
  "ConnectionStrings": {
    "DBConnection": "Server=.\\SQLEXPRESS;Database=..."
  }

}

链接问题的含义是需要提供提供者名称。我假设还需要提供其他一些 EF 配置部分。实际上,当我将 Functions 应用程序设置为启动时运行 Update-Database 时,我收到此错误:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 

这往往意味着启动项目没有正确的 EF 配置。

因此,我的问题是,我需要在 local.settings.json 文件中进行哪些额外配置?

【问题讨论】:

标签: entity-framework azure azure-functions


【解决方案1】:

您可以将提供程序名称添加到 local.settings.json 文件中的连接字符串,如下所示:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...",
  },
  "ConnectionStrings": {
    "DBConnection": {
      "ConnectionString": "Server=.\\SQLEXPRESS;Database=...",
      "ProviderName": "System.Data.SqlClient"
    }
  }
}

请注意,我取名为“DBConnection”,因为这是您在控制台应用程序中使用的名称,并且我的假设是,在您的 Azure 函数的从 DbContext 继承的上下文类中,您仍然引用相同的名称。

我的第二个假设是,由于您使用的是 SQL Express,因此您正在机器上本地测试 Azure Function。 请注意,如果您从 Azure 运行 Azure Function,则需要转到 Function App 的应用程序设置(图 1)并将连接字符串添加到 Connection strings 集合(图 2)。您必须这样做,因为在 local.settings.json 中声明的连接字符串不会部署到 Azure。

相关的微软文档可以在这里找到:local settings file documentation

图 1 - Azure Function App 应用程序设置

图 2 - 连接字符串集合

附加信息:当您在本地运行/调试您的 Azure 函数时,您可以使用 Azure 存储模拟器并避免将您的 Azure 函数绑定到由 Azure 托管的存储帐户。您只需将local.settings.json 中分配给AzureWebJobsStorageAzureWebJobsDashboard 的值替换为UseDevelopmentStorage=true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多