- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将一些 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/
.Net4.0 MVC 2 N 休眠 N单位 我正在尝试测试用户创建。从我的测试中,我调用以下内容: MembershipCreateStatus status; // _session is my
源提供者 - 列出分支: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/list%20
可以说,我想对同一个登录请求使用两个身份验证提供程序。 grails.plugins.springsecurity.providerNames = [ 'customAuthenticatio
我在本地计算机上测试了我的 webapi,它可以工作,但在 azure 上它不起作用,对于 azure,我还需要做些什么吗? 我试试这个: public Context() : base("name=
我正在尝试确定 providerName="的 ObjectContext.CommandTimeout(在 System.Data.Entity.dll 中)属性的默认值是多少System.Data
我在 NT AUTHORITY\LOCAL SYSTEM 下运行的 Windows 服务出现问题从而服务无法连接到数据库。 Login failed for user 'NT AUTHORITY\LO
我正在尝试将一些 Webjob 代码移植到新的 Azure Functions。到目前为止,我已经成功导入 DLL 并引用它们,但是当我在代码中使用连接字符串时,我收到一条错误消息,提示我必须添加 P
所以我有这段代码来处理客户端按模式发送的内容。如果他发送“getBENUds”,服务器将使用 SaveToString 方法发回该表的数据集。 然后,将其发送到客户端。 (我正在使用 Synapse
今晚我已经发疯了一段时间,试图在 java 中使用 Spring MVC 和 Hibernate 运行一个简单的项目。基本上我从一个错误到另一个错误,但它们都是由于缺少 jar 文件这一事实造成的,我
我在获取 DbContext 来从 local.settings.json 正确提取连接字符串时遇到问题 上下文: 这是一个 Azure 函数项目 主要问题代码位于System.Data.Entity
我们已经在生产环境中使用 Entity Framework 好几个月了,就在昨天,当使用我们的 DbContext 子类查询数据库时,一些机器开始出现错误: "The connection strin
我有一个首先使用 Entity Framework DB 的网站,我可以在我的本地计算机(开发计算机)上正常运行它。我有 edmx 文件和所有东西。 今天我将站点部署到 go daddy 并使用 MS
我正在尝试在无服务器 lambda(在 REST 端点中运行)中设置 AWS.config.credentials。登录来自使用 ADFS 作为联合身份的 Cognito 用户池。我还混合了 Cogn
我正在尝试在无服务器 lambda(在 REST 端点中运行)中设置 AWS.config.credentials。登录来自使用 ADFS 作为联合身份的 Cognito 用户池。我还混合了 Cogn
我正在使用 C# 读取 Windows 事件日志,我想从中选择/过滤条目。问题是事件查看器中显示的信息并不总是与我从 c# 获得的数据匹配。 例如: 事件查看器“来源”:“用户配置文件服务” 使用 E
在 App.config 中存储 MySQL 连接字符串时,providerName 属性应设置为什么值? 例如,在下面的 App.config 文件中,我应该为 providerName 使用什么值
我正在尝试使用 AWS Cognito 来帮助支持我的应用程序中的 Multi-Tenancy 。 https://aws.amazon.com/blogs/apn/managing-saas-use
我正在使用 Gatsby 构建一个静态网站,我正在使用 Nginx为静态文件提供服务。 我也在设置Docker用于将应用程序部署到生产并使用 Traefik作为 Docker 容器中的反向代理。 Tr
我在项目 Add -> New Item -> Local Database 中创建了一个 DB 文件并为其创建了一个表。 现在我想在 app.config 中为它创建一个连接字符串。 问题是我找到的
我已经使用 API“InitiateAuth”针对 Cognito 用户池进行了身份验证并收到了一个 ID token : { sub: "8aec..." , aud:"2gr0fdlr
我是一名优秀的程序员,十分优秀!