Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure: The context is being used in Code First mode with code that was generated from an EDMX

I have MVC app running fine in local. After updating to Azure, it started throwing the error:

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.

I have checked if there is any difference between the local web.config and azure web.config. except the credentials, everything is same. And it read:

<add name="DBEntities" 
     connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;data source=xx;initial catalog=xx;persist security info=True;user id=xx;password=xx;MultipleActiveResultSets=True;application name=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

I am using EF 6.1.3, MVC5

like image 802
Krishna Sarma Avatar asked Jul 07 '15 16:07

Krishna Sarma


1 Answers

I ran into the same error message and managed to solve it with the help of this post here on StackOverflow.

In the Azure Management / Web Apps / [Your web app] / CONFIGURE / connection strings , make sure of 3 things :

  1. The connection string has the same name as the connection string in your project.

  2. The connection string Value contains all of the metadata as appears in the connection string in your project. Mine looks like this:

    metadata=res://\*/Models.[MyModel].csdl|res://\*/Models.[MyModel].ssdl|res://\*/Models.[MyModel].msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:[myDBServer].database.windows.net,1433;Database=[myDB];User ID=[myDBUser]@[myDBServer];Password=[myPassword];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"

  3. The third column (default set to SQL Database) is set to Custom

like image 200
Guy Passy Avatar answered Oct 08 '22 14:10

Guy Passy