gpt4 book ai didi

entity-framework - 如何在 Azure 函数中指定 EntityFramework ProviderName

转载 作者:行者123 更新时间:2023-12-03 03:32:44 25 4
gpt4 key购买 nike

我正在尝试将一些 Webjob 代码移植到新的 Azure Functions。到目前为止,我已经成功导入 DLL 并引用它们,但是当我在代码中使用连接字符串时,我收到一条错误消息,提示我必须添加 ProviderName:

The connection string 'ConnectionString' in the application's configuration file does not contain the required providerName attribute."

这通常不是问题,因为在 Web 作业(或 Web 应用程序)中,这将位于 App 或 Web.config 中,并且连接字符串将被我在 Azure 中输入的任何内容覆盖。

使用 Azure Functions,我没有 web.config(尽管我尝试添加一个但没有成功),所以提供商名称自然会丢失。

我该如何指定?

编辑:经过一些尝试和下面的人提供的一些有用的提示后,我几乎设法让它工作。

我现在要做的事情如下:

    var connString = **MY CONN STRING FROM CONFIG**; // Constring without metadata etc.
EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
b.Metadata = "res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl";
b.ProviderConnectionString = connString.ConnectionString;
b.Provider = "System.Data.SqlClient";
return new MyDB(b.ConnectionString);

这给了我调用数据库所需的东西。我在分部类中使用静态方法来获取运行上述代码的数据库实例,并用 [DbConfigurationType(typeof(MyDbConfiguration))] 装饰 MyDB Partial

我将该配置定义为:

public class MyDBConfiguration: DbConfiguration
{
public MyDBConfiguration()
{
SetProviderFactory("System.Data.EntityClient", EntityProviderFactory.Instance);
}
}

当我想实际使用 EF 实体时,我的问题仍然存在。在这里,它将尝试使用原始配置初始化数据库类型,再次给出原始错误。根据此堆栈跟踪:

at Void Initialize()
at System.Data.Entity.Internal.EntitySetTypePair GetEntitySetAndBaseTypeForType(System.Type)
at Void InitializeContext()
at System.Data.Entity.Core.Objects.ObjectContext CreateObjectContextFromConnectionModel()
at Void Initialize()
at Boolean TryInitializeFromAppConfig(System.String, System.Data.Entity.Internal.AppConfig)
at Void InitializeFromConnectionStringSetting(System.Configuration.ConnectionStringSettings)

那么我该如何避免这种情况?我想我需要一种方法来连接所有内容并运行我的自定义 setter ..

最佳答案

最后,Stephen Reindel 将我推向了正确的方向; Entity Framework 基于代码的配置。

[DbConfigurationType(typeof(MyDBConfiguration))]
public partial class MyDB
{
public static MyDB GetDB()
{
var connString = **MY CONN STRING FROM SOMEWHERE**; // Constring without metadata etc.
EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
b.Metadata = "res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl";
b.ProviderConnectionString = connString.ConnectionString;
b.Provider = "System.Data.SqlClient";
return new MyDB(b.ConnectionString);
}

public MyDB(string connectionString) : base(connectionString)
{
}
}

像这样的 MyDbConfiguration:

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

使用上面的代码,EF 永远不会要求 AppConfig 相关的配置文件。但请记住,如果您的配置文件中有 EF 条目,它尝试使用它们,因此请确保它们已消失。

就 azure 函数而言,这意味着我使用 azure 中的 Azure Functions 配置面板来输入不带元数据和提供程序名称的 ConnectionString,然后将其加载到 GetDB 中。

编辑:根据要求,以下是上述内容的一些解释性文字:您无法在 Azure Functions 中添加有关连接的 EF 元数据,因为它们不使用 app.config 来指定它。这不是连接字符串的一部分,而是除了 EF 用于映射到特定 C# 类和 SQL 提供程序等的连接字符串之外的有关连接的元数据。为避免这种情况,您可以使用上面的示例对其进行硬编码。您可以通过创建一个继承自 DBConfiguration 的类来实现这一点,并用它来标记(使用分部类上的属性)您的 EF 数据库类。

此 DBConfiguration 包含一种不同类型的实例化新数据库对象的方法,其中此元数据是硬编码的,但连接字符串是从 Azure 中的应用程序设置中检索的。在这个例子中,我只使用了一个静态方法,但我想它也可能是一个新的构造函数。

一旦使用了这个静态方法,您就可以使用它在数据库代码中获取新数据库,如下所示:

using (var db = MyDB.GetDB()) {
// db code here.
}

这允许您在没有 APP.Config 的情况下使用 EntityFramework,并且您仍然可以使用 Azure Functions APP 设置更改连接字符串。

希望有帮助

关于entity-framework - 如何在 Azure 函数中指定 EntityFramework ProviderName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com