问题描述
我正在尝试使用现有的库,该库是使用EF 6.0连接到数据库的.net库.由于Azure Functions没有 app.config
文件,因此我尝试使用C#代码设置连接字符串.但是在使用数据库上下文连接到数据库时,出现以下异常:
I am trying to use an existing library which is a .net library which uses EF 6.0 to connect to a database. Since Azure Functions does not have an app.config
file, I am trying to set the connection string using C# code. But I am getting the following exception while connecting to the DB using my DB context:
System.ArgumentException:在已注册的.NET数据提供程序列表中找不到指定的不变名称'System.Data.SqlClient'
System.ArgumentException: The specified invariant name 'System.Data.SqlClient' wasn't found in the list of registered .NET Data Providers
MyDBContext.partial.cs:
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
.库方法将使用此方法来获取数据库上下文实例.
I have a method as following to get the DBContext
. This method will be used by the library methods to get the DB context instance.
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从数据库加载数据时,该函数内部调用上述方法以获取数据库上下文,然后连接到实际数据库.这里创建了MyDBContext对象,但是当它连接到db时,会发生以下异常.
When I execute a library method to load data from db from an Azure function v2, which internally calls the above method to get DB Context and then connects to actual DB. Here MyDBContext object is getting created, but when it connects to db the following exception occurs.
System.ArgumentException:在已注册的.NET数据提供程序列表中找不到指定的不变名称'System.Data.SqlClient'
System.ArgumentException: The specified invariant name 'System.Data.SqlClient' wasn't found in the list of registered .NET Data Providers
推荐答案
我只是解决了此问题,但使用的是Azure函数V1.在将EF与Azure函数一起使用时,可以在"local.settings.json"文件中指定连接字符串,如下所示:
I just worked on this issue but for Azure function V1.When using EF with Azure function, you can specify connection string in 'local.settings.json' file like this:
{
"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"加上实际连接字符串的提供者连接字符串"属性应该用单引号引起来(我不确定为什么微软这样做,但这应该是这样).
Please pay attention to 'ProviderName' attribute. Case should be exact as shown above and provider should be 'EntityClient'Plus 'Provider Connection String' attribute of actual connection string should be in single quote (I am not sure why Microsoft did this but this is how it is supposed to be).
这将帮助您使用EF在本地运行功能应用程序,而无需进行任何其他更改
This will help you run your function app locally with EF without any more changes
现在可以在Azure中进行部署.local.settings.json不会部署到云中.顾名思义,它充当本地运行的配置文件.因此,您需要在门户网站上的Azure函数应用程序的配置"中设置连接字符串.您可以在其中指定以下参数:
Now for deployment in Azure.local.settings.json does not get deployed to cloud. As its name suggests it acts as configuration file for local run.So you need to set connection string in 'Configuration' of Azure function app on portal.There you can specify following parameters:
Name - 'YourEntities'
value - Just Connection string part from above json file
Type - 'Custom'
Slot Settings - according to your requirement
现在,如果您发现无法在此处指定ProviderName.如果您现在尝试运行功能,将会收到缺少提供程序名称"的错误消息
Now if you notice there is no way to specify ProviderName here. If you try to run function now you will get error for 'missing provider name'
在这里,扩展的DBConfiguration类就派上用场了.
Here your extended DBConfiguration class comes in handy.
按如下所示创建数据库配置类,并将提供程序指定为EntityType
Create your DB configuration class as below and specify provider as EntityType
public class YourDBContextConfig : DbConfiguration
{
public YourDBContextConfig()
{
SetProviderServices("System.Data.EntityClient",
SqlProviderServices.Instance);
SetDefaultConnectionFactory(new SqlConnectionFactory());
}
}
您可以在为DBContext创建部分类的同一文件中创建此类.
You can create this class in same file where you have created partial class for your DBContext
向您的Context类添加以下属性:[DbConfigurationType(typeof(YourDBContextConfig))]
Add following attribute to your Context class:[DbConfigurationType(typeof(YourDBContextConfig))]
还要确保您的部分上下文类具有构造函数,该构造函数将连接字符串作为参数并在初始化上下文时提供该字符串:
Also make sure your partial context class has constructor that takes connection string as parameter and supply it while initializing context:
string connString =
ConfigurationManager.ConnectionStrings["YourEntities"].ConnectionString;
using (YourEntities db = new YourEntities(connString))
{
}
这将适用于部署.
这篇关于从Azure函数V2连接到SQL Server时,Entity Framework 6出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!