Database laser publishing The first connection string does not work

I took steps to publish my web application using the database first on the azure portal.

However, when I post a message, I get the following error message:

Code generated using T4 templates for the First and Model databases The first development may not work correctly if used in Code First mode. To continue using the First or Model First database, make sure that the Entity Server Connection String is specified in the application execution configuration file. To use these classes that were generated from Database or Model First, with code First add additional configurations using the attributes or the DbModelBuilder API, and then remove the code that throws this exception.

My connection string in web.config after it has been changed by the publication:

<add name="MySiteEntities" connectionString="metadata=res://*/MySite.csdl|res://*/MySite.ssdl|res://*/MySite.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:**********.database.windows.net,****;initial catalog=MySite;user id=username@ **********;password=*******;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> 

My context (generated by edmx):

 public partial class MySiteEntities : DbContext { public MySiteEntities() : base("name=MySiteEntities") { } ... 

I am very confused because it seems that the framework entity is trying to use the code first, not the database first.

UPDATE: I just tried using the same connection string locally, and the web application seems to be working fine. The web application really connects to the remote database. Only when I publish on the azure image does it fail.

+4
source share
3 answers

Read my answer to a similar question in Working with Entity Infrastructure, but not Azure .

If you made the same “mistake” you did, this is what happens ... the application deployed by Azure does not find your connection string “MySiteEntities” inside your web.config. Instead, when you created your Azure Web Site (or a cloud service or something else), you created the linked Azure SQL database and pointed to the connection string with the same name "MySiteEntities". This last connection string is a “normal” connection string without reference to Model / Database-first metadata, and therefore it is treated as the code-first EF connection, which then complains about the conflict. For an explanation of this difference, see Code First vs. Database First .

+4
source

It should be:

  <connectionStrings> <add name="MyDatabaseModelEntities" connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl; provider=System.Data.SqlClient; provider connection string=&quot; Data Source=<provideServerName>.database.windows.net; Initial Catalog=MyDatabase; Integrated Security=False; User ID=<provideUserID>; Password=providePassword>; MultipleActiveResultSets=True; Encrypt=True; TrustServerCertificate=False&quot;" providerName="System.Data.EntityClient"/> </connectionStrings> 
0
source

I changed the connection string to remote (Azure) on my local web.config, and then deleted all the installed connection strings at the time of publication and published web.config. It overwrites the removal of web.config. Then return the connection string to the local web.config for local connection. Now it works great.

0
source

All Articles