Entity framework 实体框架未检测Azure函数的DB连接字符串
使用使用EF连接到DB的控制台应用程序,我能够调用Entity framework 实体框架未检测Azure函数的DB连接字符串,entity-framework,azure,azure-functions,Entity Framework,Azure,Azure Functions,使用使用EF连接到DB的控制台应用程序,我能够调用更新数据库,并让它连接到DB。但是,我在尝试将其迁移到Azure函数时遇到了问题。有人问了一个类似的问题,我怀疑解决方案可能是相同的,尽管没有太多关于这可能是什么的细节 在我的控制台应用程序中,提供了以下相关配置: <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.In
更新数据库
,并让它连接到DB。但是,我在尝试将其迁移到Azure函数时遇到了问题。有人问了一个类似的问题,我怀疑解决方案可能是相同的,尽管没有太多关于这可能是什么的细节
在我的控制台应用程序中,提供了以下相关配置:
<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>
链接问题的含义是需要提供提供者名称。我假设还需要提供一些其他EF配置部分。事实上,当我在函数应用程序设置为启动的情况下运行updatedatabase
时,我得到以下错误:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly
这往往意味着启动项目没有正确的EF配置
因此,我的问题是,我需要在local.settings.json文件中进行哪些额外配置?您可以将提供者名称添加到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”,是因为这是您在控制台应用程序中使用的名称,我的假设是,在从DbContext继承的Azure函数的上下文类中,您仍然引用相同的名称
我的第二个假设是,您正在计算机上本地测试Azure函数,因为您使用的是SQL Express。
请注意,如果从Azure运行Azure函数,则需要转到函数应用程序的应用程序设置(图1),并将连接字符串添加到连接字符串
集合(图2)。您必须这样做,因为local.settings.json
中声明的连接字符串将不会部署到Azure
相关的Microsoft文档可在此处找到:
图1-Azure功能应用程序设置
图2-连接字符串集合
其他信息:当您在本地运行/调试Azure功能时,您可以使用Azure存储模拟器,避免将Azure功能绑定到Azure托管的存储帐户。您只需在
local.settings.json中替换azurewebjobstorage
和AzureWebJobsDashboard
中分配给UseDevelopmentStorage=true
的值。您是否检查了此链接-,Azure函数是唯一可以作为Startup运行的项目。您可以尝试指定providerName
和连接字符串。
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...",
"AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...",
},
"ConnectionStrings": {
"DBConnection": {
"ConnectionString": "Server=.\\SQLEXPRESS;Database=...",
"ProviderName": "System.Data.SqlClient"
}
}
}