2

I have an Azure Function that uses database first approach connection string. The connection string is located in the local.settings.json file, which contains the sensitive data.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "MyConnectionString": "data source=localhost\\sqlexpress;initial catalog=SampleQrCodes;user id=sa;password=****;MultipleActiveResultSets=True;App=EntityFramework"
  }
} 

If I try removing providerName it gives me the following exception:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly.....you pass it to one of the base DbContext constructors that take a DbConnection

Actual connection string (generated from EF):

<add name="DataContext" connectionString="metadata=res://*/EFModel.csdl|res://*/EFModel.ssdl|res://*/EFModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\sqlexpress;initial catalog=SampleQrCodes;user id=sa;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Here is similar question Load Connection String from Config File in Azure Functions but it is not working for me how to define this connection string in local.settings.json via database first approach?

JRB
  • 1,943
  • 1
  • 10
  • 9
Kumari Dimple
  • 343
  • 2
  • 4
  • 14

1 Answers1

2

According to your description, here is the similar git hub issue. Per my test, you could configure your connection string within the local.settings.json file as follows:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>",
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>"
  },
  "ConnectionStrings": {
    "DataContext": {
      "ConnectionString": "metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=<server-name>.database.windows.net;initial catalog=<database-name>;persist security info=True;user id=<username>;password=<password>;MultipleActiveResultSets=True;App=EntityFramework'",
      "ProviderName": "System.Data.EntityClient"
    }
  }
} 

For deploying to your azure function, you may need to modify your DbContext. More details, you could refer to this similar issue.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35