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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.