Omar Lenny
Omar Lenny

Reputation: 51

Azure Publish Database First Connection String Not Working

I have gone through the steps to publish my web app using database first on the azure portal.

However, when I publish I get this error message:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

My connection string in the web.config after it has been modified by publish:

<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 becuase it seems like entity framework is trying to use code first rather than database first.

UPDATE: I just tried using the same connection string locally and the web app seems to run fine. The web app does connect to the remote database fine. It is only when I publish to azure it fails.

Upvotes: 4

Views: 4573

Answers (3)

Oleg Sh
Oleg Sh

Reputation: 9013

I changed connection string to remote (Azure) on my local web.config, then remove all set connection strings during publishing and publish web.config. It rewrites remove web.config. Then return connection string on local web.config to local connection. It works fine now.

Upvotes: 0

Nathan Watson
Nathan Watson

Reputation: 216

Read my answer to a similar question at Entity framework work locally but not on azure.

If you made the same "mistake" I did, this is what's happening ... the Azure-deployed app isn't finding your connection string "MySiteEntities" inside your web.config. Instead, at the time your created your Azure Web Site (or Cloud Service or whatever), you created an associated Azure SQL Database and gave its connection string the exact same name, "MySiteEntities". This latter connection string is a "plain" connection string without Model/Database-first metadata references, and so is being treated as a Code-First connection by EF, which then complains about the conflict. See Code First vs. Database First for an explanation of this distinction.

Upvotes: 4

Matija Grcic
Matija Grcic

Reputation: 13381

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>

Upvotes: 0

Related Questions