0

I am deploying a site to Azure that uses EF6 using the VS Studio publish option.

I am using the default behavior for for database naming rather than specifying a connection string as I do development from multiple machines which have a mixture of localDB or SQL Express:

public WebsiteDBContext() : base("WebsiteDBContext") 

The EF code is all in a separate project to the website as it is used on multiple websites sharing the same DB.

When I publish to Azure the connection strings added to web.config include a namespace from the project the DBContext code is in:

<add name="Utils.Models.WebsiteDBContext" connectionString="Data Source=****.database.windows.net;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=****;Password=****" providerName="System.Data.SqlClient" /> <add name="DefaultConnection" connectionString="Data Source=tcp:****.database.windows.net,1433;Initial Catalog=CreweAllen;User ID=****;Password=****" providerName="System.Data.SqlClient" /> <add name="Utils.Models.WebsiteDBContext_DatabasePublish" connectionString="Data Source=****.database.windows.net;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=****;Password=****" providerName="System.Data.SqlClient" /> 

The Migration fails to run I think because it can't find the database.

If I manually create the tables and remove the migration table creations the website fails with: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation.

But if edit the web.config and remove "Utils.Models." from the WebsiteDBContext connection string but not the WebsiteDBContext_DatabasePublish it connects to the database ok.

How can I get the connections correctly named and the migration to run? Have tried hard but failed to find a solution.

Thanks

1
  • I would strongly recommend not to use the VS publish option. Its not prime for daily use because it introduces odd scenarios like this. I would recommend FTP or CICD. Commented Apr 12, 2019 at 6:24

2 Answers 2

1

Eventually resolved it.

The published application couldn't find a connection string with the name it was looking for "WebsiteDBContext" as the VS deployment wizard was naming the connection string "Utils.Models.WebsiteDBContext" so it was falling back to the default EF connection which was localDB:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 

When I stopped explicitly naming the DB name in the DBContext:

public WebsiteDBContext() : base() 

Then the name of the connection the app looked for was "Utils.Models.WebsiteDBContext" which it found.

So what doesn't work well is publishing to Azure through the VS wizard with named DBContext as the connection strings created on Azure have the full namespace.

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

1 Comment

The connection string added by Visual Studio via settings for Windows Forms app adds full namespace as well, it's not only for Azure.
0

Your context class is named WebsiteDBContext. By convention, you'll have a connection string in your web.config named WebsiteDBContext. When you enable code migrations via this wizard, the wizard will create a second connection string named WebsiteDBContext_DatabasePublish that will only be used for running your code first migrations.

Update your DbContext which the datapublish stuff is still in the webconfig but no I'm no longer getting the exception.

 public WebsiteDBContext():base("WebsiteDBContext", throwIfV1Schema: false) // throwIfV1Schema:false was added as the Identity V2 was causing an error in Azure 

For more details you could refer to this issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.