Missing ProviderName when debugging AzureFunction as well as deploying azure function

前端 未结 4 752
忘了有多久
忘了有多久 2020-11-29 09:30

I have an issue getting a DbContext to correctly pull my connection string from my local.settings.json

Context:

  • This is an Az
4条回答
  •  死守一世寂寞
    2020-11-29 10:05

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

    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
    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"
      }
    }  
    

    These points are important:

    • 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

    Fix for missing providername in deployment

    Note, this answer assumes you are trying to use the parameterless constructor of a DbContext. If you are creating new code you can easily follow the second upvoted answer

    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;
        }  
    

    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!

    EDIT

    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.

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

    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

提交回复
热议问题