Entity framework 从Azure function V2连接到SQL Server时实体框架6出现问题
我正在尝试使用一个现有的库,它是一个使用EF 6.0连接到数据库的.net库。由于Azure函数没有Entity framework 从Azure function V2连接到SQL Server时实体框架6出现问题,entity-framework,azure,azure-functions,Entity Framework,Azure,Azure Functions,我正在尝试使用一个现有的库,它是一个使用EF 6.0连接到数据库的.net库。由于Azure函数没有app.config文件,因此我尝试使用C#code设置连接字符串。但在使用我的DB上下文连接到DB时,我遇到以下异常: System.ArgumentException:具有固定名称“System.Data.SqlClient”的ADO.NET提供程序未在计算机或应用程序配置文件中注册,或者无法加载。有关详细信息,请参见内部异常 System.ArgumentException:在已注册的.NE
app.config
文件,因此我尝试使用C#code设置连接字符串。但在使用我的DB上下文连接到DB时,我遇到以下异常:
System.ArgumentException:具有固定名称“System.Data.SqlClient”的ADO.NET提供程序未在计算机或应用程序配置文件中注册,或者无法加载。有关详细信息,请参见内部异常
System.ArgumentException:在已注册的.NET数据提供程序列表中找不到指定的不变名称“System.Data.SqlClient”
MyDBContext.partial.cs:
[DbConfigurationType(typeof(MyDbConfiguration))]
public partial class MyDBContext : DbContext
{
public MyDBContext (string ConnectionString)
: base(ConnectionString)
{
}
}
public class MyDbConfiguration : DbConfiguration
{
public MyDbConfiguration()
{
SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
SetDefaultConnectionFactory(new SqlConnectionFactory());
}
}
我有如下方法来获取DBContext
。库方法将使用此方法来获取DB上下文实例
public MyDBContext GetDB( string metadata, string connectionString )
{
EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
b.Metadata = metadata;
b.ProviderConnectionString = connectionString;
b.Provider = "System.Data.SqlClient";
return new MyDBContext (b.ConnectionString);
}
当我执行一个库方法从Azure函数v2从db加载数据时,该函数在内部调用上述方法以获取db上下文,然后连接到实际的db。这里创建的是MyDBContext对象,但当它连接到db时,会发生以下异常
System.ArgumentException:具有固定名称“System.Data.SqlClient”的ADO.NET提供程序未在计算机或应用程序配置文件中注册,或者无法加载。有关详细信息,请参见内部异常
System.ArgumentException:在已注册的.NET数据提供程序列表中找不到指定的不变名称“System.Data.SqlClient”
我刚刚研究过这个问题,但是针对Azure function V1。 将EF与Azure函数一起使用时,可以在“local.settings.json”文件中指定连接字符串,如下所示:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": ""
},
"ConnectionStrings": {
"YourEntities": {
"ConnectionString": "metadata=res://*/EF.yourModel.csdl|res://*/EF.yourModel.ssdl|res://*/EF.yourModel.msl;provider=System.Data.SqlClient;provider connection string='data source=yourServer;initial catalog=yourDB;persist security info=True;user id=yourUserID;password=yourPwd;MultipleActiveResultSets=True;App=EntityFramework'",
"ProviderName": "System.Data.EntityClient"
}
}
}
请注意“ProviderName”属性。案例应如上所示,且提供程序应为“EntityClient”
加上实际连接字符串的“Provider Connection String”属性应该在单引号中(我不确定Microsoft为什么这样做,但应该是这样的)
这将帮助您使用EF在本地运行函数应用程序,而无需进行任何更改
现在在Azure中部署。
local.settings.json未部署到云。顾名思义,它充当本地运行的配置文件。
因此,您需要在门户上Azure function app的“配置”中设置连接字符串。
您可以在其中指定以下参数:
Name - 'YourEntities'
value - Just Connection string part from above json file
Type - 'Custom'
Slot Settings - according to your requirement
现在,如果您注意到这里没有办法指定ProviderName。如果您现在尝试运行函数,您将得到“缺少提供程序名称”的错误
在这里,扩展的DBConfiguration类很方便
如下所示创建DB配置类,并将提供程序指定为EntityType
public class YourDBContextConfig : DbConfiguration
{
public YourDBContextConfig()
{
SetProviderServices("System.Data.EntityClient",
SqlProviderServices.Instance);
SetDefaultConnectionFactory(new SqlConnectionFactory());
}
}
可以在为DBContext创建分部类的同一文件中创建此类
将以下属性添加到上下文类:
[DbConfigurationType(typeof(YourDBContextConfig))]
还要确保您的部分上下文类具有以连接字符串为参数的构造函数,并在初始化上下文时提供该构造函数:
string connString =
ConfigurationManager.ConnectionStrings["YourEntities"].ConnectionString;
using (YourEntities db = new YourEntities(connString))
{
}
这将适用于部署。使用AzureFunctions中可用的
应用程序设置
或连接字符串
检索连接字符串。你不必做这些事,非常感谢你的回复。。!但是这里我想使用Azure functions V2,所以我将Entityframework版本6升级到了6.3,问题得到了解决。但在此之后,我在连接到数据库时遇到另一个错误。错误消息:“MethodCallTranslator”的类型初始值设定项引发了异常。“类型初始值设定项错误通常意味着静态成员存在一些初始化问题,但在您刚刚更新EF时,情况似乎并非如此。我想这是EF更新的问题。您是否再次尝试卸载并安装EF?