0

I have an Azure function application that when I debug it in my local PC, it won't give any error and will work correctly, but when I deploy it into the Azure Functions web, it will throw an error indicating that my Entities connection doesn't have the ProviderName specified.

I followed the solutions given by Missing ProviderName when debugging AzureFunction as well as deploying azure function, I have created a myDBContextConfig and added it to the Auto Generated dbContext file from my .edmx, but I still continue to have the same problem after deploying.

Here are some screenshots and config data I'm using:

the Error: The Provider Name Error

The local.settings.json:

{ "IsEncrypted": false,
  "Values": { 
             //Some Values
  },
 "ConnectionStrings": {
  "Entities": {
  "ConnectionString": "metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string='data source=ES-HHASQL01\\SQLES;initial catalog=Entities;persist security info=True;Integrated Security=False;User ID=****;Password=****;MultipleActiveResultSets=True;application name=EntityFramework'",
  "ProviderName": "System.Data.EntityClient"
    }
  } 
}

And the Azure functions connection String:

enter image description here

The hidden connection follows the style of the quoted text below.

metadata=res:///Models.myDB.csdl|res:///Models.myDB.ssdl|res://*/Models.myDB.msl;provider=System.Data.SqlClient;provider connection string='data source=[yourdbURL];initial catalog=myDB;persist security info=True;user id=xxxx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework

If any one can help me with this I would really appreciate it. Thank you.

Edit:

Well I have tested the issue with the code provided by @Joey Cai, and made the next changes:

the new App Settings connection string is has follows:

Data Source=tcp:*.database.windows.net,1433;Initial Catalog=***MultipleActiveResultSets=true;User ID=*;Password=***;Connection Timeout=30;App=EntityFramework;

I added the database configuration class to the Auto generated code from the dbContext has follows:

 [DbConfigurationType(typeof(EntitiesConfig))]
public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
}

and created the class has especified by: @Joey Cai

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

But know I'm getting this error when running the azure function only if the connection type is SQLServer or SQLAzure, if it's set to Custom it still shows the ProviderNamer error shown before: New Error

1 Answers1

0

You can't add EF metadata about the connection in Azure Functions, as they do not use an app.config in which to specify it. This is not a part of the connection string, but is metadata about the connection besides the connection string that EF uses to map to a specific C# Class and SQL Provider etc. To avoid this, you could following the code as below.

[DbConfigurationType(typeof(DBContextConfig))]
partial class 
{

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

public class DBContextConfig : DbConfiguration
{
    public DBContextConfig()
    {
        SetProviderServices("System.Data.EntityClient", SqlProviderServices.Instance);
        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}
Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Would it be Possible to use the DbConfiguration class with the auto generated dbContext? – Andres Scarpone Mar 15 '19 at 07:34
  • ok, so following your suggestion it should look like this: [DbConfigurationType(typeof(EntitiesConfig))] public partial class Entities : DbContext { public Entities() : base("name=Entities") { } } – Andres Scarpone Mar 15 '19 at 07:47
  • Ok, just tested it and still got the error, it's different since I change the connection string, would you please check it out, I eddited the post to add this info in the last part of it. – Andres Scarpone Mar 15 '19 at 08:08
  • Why do your connection string changed? It should be like the `metadata=res,,,,,,,,,` which contains all of the metadata as appears in the connection string in your project. – Joey Cai Mar 15 '19 at 08:50
  • Sorry, I thought I needed to use the connection without the Metadata, I have changed it back into the original connection String but still have the same problem. – Andres Scarpone Mar 15 '19 at 09:24