调试AzureFunction以及部署Azure函数时缺少ProviderName [英] Missing ProviderName when debugging AzureFunction as well as deploying azure function

查看:83
本文介绍了调试AzureFunction以及部署Azure函数时缺少ProviderName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取DbContext以便从我的local.settings.json

上下文:

  • 这是一个Azure功能项目
  • 主要问题代码在System.Data.Entity.Internal.AppConfig
  • 尽管我有一个local.settings.json文件,但这不是dotnet核心.是.net 4.6.1
  • This is an Azure function project
  • The main problem code is in System.Data.Entity.Internal.AppConfig
  • Although I have a local.settings.json file this is not dotnet core. It's .net 4.6.1

错误消息:

应用程序配置文件中的连接字符串'ShipBob_DevEntities'不包含必需的providerName属性."'

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

Json配置:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": ""
},

"ConnectionStrings": {
"ShipBob_DevEntities": {
  "ConnectionString": "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=***;initial catalog=***;persist security info=True;User Id=***;Password=***;;multipleactiveresultsets=True;application name=EntityFramework'",
  "providerName": "System.Data.EntityClient"
    }
  }
}  

已测试的配置版本:

  • 将提供者名称移动到实际的ConnectionString令牌值中:发生相同的错误
  • ConnectionString属性内的provider属性设置为EntityClient:此操作无济于事
  • ShipBob_DevEntities的字符串值=设置为ConnectionString的值:这会引发新的错误,例如

  • Moving the provider name into the actual ConnectionString token value : same error ocurrs
  • Setting the provider attribute inside the ConnectionString attribute to EntityClient: this did nothing
  • Making ShipBob_DevEntities a string value = to the value of ConnectionString : this throws new errors the likes of which are

不支持关键字元数据

keyword metadata is not supported

  • 我尝试使用ADO连接字符串,该字符串会引发code first异常,当database first方法中的连接字符串不正确时,该异常似乎会发生.

  • I tried using an ADO connection string which throws a code first exception which seems to occur when your connection string is incorrect in a database first approach.

    我已经自由使用 dotPeek 来反编译EntityFramework.dll并将问题追溯到System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig.在此方法内,有一个对LazyInternalConnection.FindConnectionInConfig的调用,它弹出一个ConnectionStringSettings对象,该对象的ProviderName值设置为null.不幸的是,我无法调试AppConfig.cs类,该类似乎用于生成该值,所以我陷入了困境.

    I've taken the liberty to decompile EntityFramework.dll using dotPeek and have traced the problem down to System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig. Inside this method there is a call to LazyInternalConnection.FindConnectionInConfig which spits out a ConnectionStringSettings object that has it's ProviderName value set to null. Unfortunately I am unable to debug the AppConfig.cs class which it seems to use to generate this value so I am stuck.

    到目前为止,我已经参考了这两篇文章.其中之一规定将提供者名称作为其自己的令牌.但是,这是行不通的.

    So far I have consulted these two articles. One of which states to put the provider name as it's own token; however, this is not working.

    https://github.com/Azure/azure-functions-cli/Issues/193
    https://github.com/Azure/azure-functions-cli/issues/46

    有人知道用于Entity Framework连接的local.settings.json中使用的正确格式吗?

    Does anyone know the correct format to use in local.settings.json for an Entity Framework connection?

    推荐答案

    因此解决方案最终变得微不足道.在local.settings.json 必须中指定的ProviderName属性为驼峰式大小写.

    So the solution ended up being trivial. The ProviderName attribute specified in local.settings.json MUST be camel case.

    来自最初的git hub讨论:
    https://github.com/Azure/azure-functions-cli/issues/46
    将提供者名称显示为Pascal大小写

    From the original git hub discussions :
    https://github.com/Azure/azure-functions-cli/issues/46
    Shows the provider name as being pascal case

    https://github.com/Azure/azure-functions-cli/Issues/193
    以伪代码显示提供者名称为驼峰大小写 很容易错过,但是您的config部分必须完全如下

    https://github.com/Azure/azure-functions-cli/issues/193
    Shows the provider name being camel case in pseudo code It was very easy to miss but your config section must be exactly as follows

    "ConnectionStrings": {
    "ShipBob_DevEntities": {
      "ConnectionString": "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=***;initial catalog=***;persist security info=True;User Id=***;Password=***;;multipleactiveresultsets=True;application name=EntityFramework'",
      "ProviderName":  "System.Data.EntityClient"
      }
    }  
    

    这些要点很重要:

    • 确保您的连接字符串包含元数据信息
    • 如果要从xml配置中复制字符串,请确保不对撇号进行转义
    • 确保ProviderName属性是驼色情况
    • 确保提供者名称为System.Data.EntityClient
    • Make sure your connection string has metadata information
    • If copying your string from an xml config, make sure you unescape apostrophes
    • Make sure the ProviderName attribute is camel case
    • Make sure the provider name is System.Data.EntityClient

    注意,此答案假定您正在尝试使用DbContext的无参数构造函数.如果您要创建新代码,则可以轻松遵循第二个建议的答案

    我想出了一种方法来解决提供程序名称问题,同时仍然保留对门户网站配置的使用,从而保留了部署槽.它涉及使用静态属性设置数据库上下文的默认连接字符串

    I figured out a way to circumvent the provider name issue while still retaining the use of the portal config and thus deployment slots. It involves setting the default connection string of db context using static properties

    private static string _connectionString = "name=ShipBob_DevEntities";
    
        static ShipBob_DevEntities()
        {
            if(!string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AzureFunction")))
            {
                var connectionString = System.Environment.GetEnvironmentVariable("EntityFrameworkConnectionString");
    
                if (!string.IsNullOrEmpty(connectionString))
                {
                    _connectionString = connectionString;
                }
            }
        }
    
        public ShipBob_DevEntities()
            : base(_connectionString)
        {
            this.Configuration.LazyLoadingEnabled = false;
        }  
    

    这涉及开发人员在azure门户中创建应用程序设置作为标志.就我而言,它是 AzureFunction .这样可以确保我们的代码仅在azure函数中运行,并且此DbContext的所有其他客户端(无论是Web应用程序,Windows应用程序等)都可以继续按预期方式运行.这还涉及将您的连接字符串作为 AppSetting 而不是实际的连接字符串添加到azure门户.请使用完整的连接字符串,包括元数据信息,但不要提供提供商名称!

    This involves the developer to create an app setting in the azure portal as a flag. In my case it is AzureFunction. This makes sure that our code is only run in an azure function and all other clients of this DbContext, whether they be web apps, windows apps, etc, can still continue behaving as expected. This also involves adding your connection string to the azure portal as an AppSetting and not an actual connection string. Please use the full connection string including them metadata information but without the provider name!

    如果要先使用db,则需要编辑自动生成的.tt文件t4模板,以确保此代码不会被覆盖.

    You will need to edit your auto generated .tt file t4 template to make sure this code does not get overridden if you are using db first.

    这里是T4语法的链接: https://docs.microsoft.com/zh-CN/visualstudio/modeling/writing-a-t4-text-template

    Here is a link on the T4 syntax: https://docs.microsoft.com/en-us/visualstudio/modeling/writing-a-t4-text-template

    这是有关EF T4模板的说明:

    And here is an explanation on EF T4 templates: https://msdn.microsoft.com/en-us/library/jj613116(v=vs.113).aspx#1159a805-1bcf-4700-9e99-86d182f143fe

    这篇关于调试AzureFunction以及部署Azure函数时缺少ProviderName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆