2

Using a console app that connected to a DB using EF, I was able to call Update-Database and have it connect to the DB. However, I'm having trouble when trying to migrate this over to an Azure function. A similar question was asked here, and I suspect the solution might be the same, although there's not too much detail on what that might be.

In my console app, the following relevant configuration was supplied:

<configuration>
  <configSections>    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <connectionStrings>
      <add name="DBConnection" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyDB;User Id= . . ." />
    </connectionStrings>
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
    </entityFramework>

In my local.settings.json, I have only the DB Connection String:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...",
  },
  "ConnectionStrings": {
    "DBConnection": "Server=.\\SQLEXPRESS;Database=..."
  }

}

The implication in the linked question, is that the provider name needs to be supplied. I would assume that some of the other EF config sections would also need to be supplied. As it is, when I run Update-Database with the Functions app set as start-up, I get this error:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 

Which tends to just mean that the start-up project doesn't have the correct EF configuration.

My question, therefore, is what additional configuration do I need in my local.settings.json file?

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • Did you check this link - https://stackoverflow.com/questions/38436256/enable-migrations-exception-calling-setdata-with-2-arguments ? – Jatin Parmar Feb 15 '18 at 11:24
  • Unfortunately, the Azure function is the only project that can function as a startup – Paul Michaels Feb 15 '18 at 11:30
  • You can try to specify `providerName` and connection string [programmatically](https://stackoverflow.com/questions/39916981/how-to-specify-entityframework-providername-in-an-azure-function#answer-39975970). – Slava Utesinov Feb 16 '18 at 12:00

1 Answers1

0

You can add the provider name to your connection string in the local.settings.json file as shown here:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=...",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=...",
  },
  "ConnectionStrings": {
    "DBConnection": {
      "ConnectionString": "Server=.\\SQLEXPRESS;Database=...",
      "ProviderName": "System.Data.SqlClient"
    }
  }
}

Note that I took the name "DBConnection" because this is what you had in your console application and my assumption is that in your context class, that inherits from DbContext, of your Azure Function you are still referencing the same name.

My second assumption is that you are testing you Azure Function locally on your machine because you are using SQL Express. Pay attention that if you run your Azure Function from Azure, then you need to go to your Function App's application settings (image 1) and add your connection string to the Connection strings collection (image 2). You have to do this because the connection strings declared in your local.settings.json will not be deployed to Azure.

The related Microsoft documentation can be found here: local settings file documentation.

Azure Function App application settings

Image 1 - Azure Function App application settings

Connection strings collection

Image 2 - Connection strings collection

Additional info: when you locally run/debug your Azure Function, you could use the Azure Storage Emulator and avoid binding your Azure Function to a storage account hosted by Azure. You just have to replace in your local.settings.json the values assigned to AzureWebJobsStorage and AzureWebJobsDashboard by UseDevelopmentStorage=true.

Aquablue
  • 294
  • 3
  • 4