【问题标题】:Issue with Entity Framework 6 while connecting to SQL Server from Azure function V2从 Azure 函数 V2 连接到 SQL Server 时出现实体框架 6 的问题
【发布时间】:2019-09-25 18:49:28
【问题描述】:

我正在尝试使用现有的库,它是一个使用 EF 6.0 连接到数据库的 .net 库。由于 Azure Functions 没有 app.config 文件,因此我尝试使用 C# 代码设置连接字符串。但是在使用我的数据库上下文连接到数据库时出现以下异常:

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。库方法将使用此方法来获取数据库上下文实例。

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 Context,然后连接到实际的 DB。这里正在创建 MyDBContext 对象,但是当它连接到 db 时会发生以下异常。

System.ArgumentException:具有不变名称“System.Data.SqlClient”的 ADO.NET 提供程序未在计算机或应用程序配置文件中注册,或者无法加载。有关详细信息,请参阅内部异常。

System.ArgumentException:在已注册的 .NET 数据提供程序列表中找不到指定的不变名称“System.Data.SqlClient”

【问题讨论】:

  • 使用 AzureFunctions 中可用的 applicationSettingsConnectionStrings 来检索您的连接字符串。您不必执行任何这些操作

标签: entity-framework azure azure-functions


【解决方案1】:

我刚刚解决了这个问题,但针对的是 Azure 函数 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” 加上实际连接字符串的“提供者连接字符串”属性应该是单引号(我不知道微软为什么这样做,但这是应该的)。

这将帮助您使用 EF 在本地运行函数应用,而无需进行任何更改

现在在 Azure 中进行部署。 local.settings.json 不会部署到云端。顾名思义,它充当本地运行的配置文件。 因此,您需要在门户上 Azure 函数应用的“配置”中设置连接字符串。 您可以在此处指定以下参数:

Name - 'YourEntities'
value - Just Connection string part from above json file
Type - 'Custom'
Slot Settings - according to your requirement

现在,如果您注意到这里无法指定 ProviderName。如果您现在尝试运行函数,您将收到“缺少提供者名称”的错误

你的扩展 DBConfiguration 类就派上用场了。

如下创建您的数据库配置类,并将提供者指定为 EntityType

public class YourDBContextConfig : DbConfiguration
{
    public YourDBContextConfig()
    {
    SetProviderServices("System.Data.EntityClient",
    SqlProviderServices.Instance);
    SetDefaultConnectionFactory(new SqlConnectionFactory());
    }

}

您可以在为 DBContext 创建部分类的同一文件中创建此类

将以下属性添加到您的 Context 类: [DbConfigurationType(typeof(YourDBContextConfig))]

还要确保您的部分上下文类具有将连接字符串作为参数并在初始化上下文时提供它的构造函数:

string connString = 
ConfigurationManager.ConnectionStrings["YourEntities"].ConnectionString;
using (YourEntities db = new YourEntities(connString))
{
}

这将适用于部署。

【讨论】:

  • 非常感谢您的回复..!但在这里我想使用 Azure 函数 V2,所以我将 Entityframework 版本 6 升级到 6.3 并解决了问题。但在那之后,我在连接到数据库时遇到了另一个错误。错误消息:“'MethodCallTranslator' 的类型初始化程序引发了异常。”
  • 类型初始值设定项错误通常意味着静态成员的一些初始化问题,但在您刚刚更新 EF 时似乎并非如此。我想这是 EF 更新问题。您是否尝试再次卸载并安装 EF?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多