How to use Managed Identity for EF6?

danial tham 11 Reputation points Microsoft Employee
2022-09-28T11:10:29.097+00:00

Hi,

We are currently trying to migrate our database and application from on prem to AppService and Azure SQL, for the db access code it's EDMX based Entity Framework 6.
To connect to the database server from app service we tried to use managed identity following the steps in this in this doc but couldn't get it to work.

The error we are getting is

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. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

Any help on this would be appreciated/

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,875 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,473 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. danial tham 11 Reputation points Microsoft Employee
    2022-09-29T02:50:13.553+00:00

    Found the answer, if you're following the [doc][1], you would have inevitably tried to use this "server=tcp:<server-name>.database.windows.net;database=<db-name>;" format for your connection string. Which is NOT RIGHT, for EDMX based connection string, you just need to swap our the server value and the database name in your existing connection string removing all the user id and password from it. The working connection string should look similar to below in your web.config:

    "metadata=res://*/AdventureWorks.csdl|res://*/AdventureWorks.ssdl|res://*/AdventureWorks.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=yourdbserver.database.windows.net;initial catalog=yourdbtable;MultipleActiveResultSets=True;App=EntityFramework&quot;"  
    
    • NOTE: the " in the value should be substituted with actual single quote if the conn string is injected through Azure App Configuration

    https://learn.microsoft.com/en-us/azure/app-service/tutorial-connect-msi-sql-database?tabs=windowsclient%2Cef%2Cdotnet#3-modify-your-project

    1 person found this answer helpful.