How to specify EntityFramework ProviderName in an Azure Function

前端 未结 6 620
梦毁少年i
梦毁少年i 2020-12-31 15:32

I\'m trying to port some webjob code to the new Azure Functions. So far I\'ve managed to import my DLL\'s and reference them succesfully, but when I use the connection strin

6条回答
  •  隐瞒了意图╮
    2020-12-31 16:24

    In the end, Stephen Reindel pushed me in the right direction; Code-based Configuration for 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)
       {
       }
    }
    

    With MyDbConfiguration like this:

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

    With the above code, EF never asks for AppConfig-related config files. But remember, if you have EF entries in your config file, it will attempt to use them, so make sure they're gone.

    In terms of azure functions, this means I used the Azure Functions configuration panel in azure to punch in my ConnectionString without the Metadata and providername, and then loaded that in GetDB.

    Edit: As per request, here is some explanatory text of the above: 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 hardcode it using the above example. You do that by creating a class inheriting from DBConfiguration, and you mark (with an attribute on a partial class) your EF database class with that.

    This DBConfiguration contains a different kind of way to instantiate a new database object, in which this metadata is hardcoded, but the connectionstring is retrieved from your app settings in Azure. In this example I just used a static method, but I guess it could be a new constructor also.

    Once you have this static method in play, you can use that to get a new database in your database code, like this:

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

    This allows you to use EntityFramework without an APP.Config, and you can still change the connectionstring using Azure Functions APP settings.

    Hope that helps

提交回复
热议问题