Connection strings adding namespace when deploying Entity Framework to Azure - entity-framework

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

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.

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.

Related

Azure Deployment Entity Framework Connection String Keyword not supported: 'metadata'

I have been developing an application locally using asp and entity framework to interact with my database. I am using a model first approach. I want to deploy this app to Azure but I keep getting the error "Keyword not supported: 'metadata'."
Through searching and reading posts like this: Windows Azure, Entity Framework. Keyword not supported: 'metadata'.
I know its an issue with my connection strings but i can't figure out what i'm doing wrong.
Connection string in my local web config:
<add name="BeCivicData" connectionString="metadata=res://*/Models.BeCivicData.csdl|res://*/Models.BeCivicData.ssdl|res://*/Models.BeCivicData.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=BeCivic;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
Generated Connection String for the Azure DB:
Server=tcp:becivicserver.database.windows.net,1433;Initial Catalog=BeCivic;Persist Security Info=False;User ID={MyID};Password={MyPass}5;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
On the Azure Applications settings page i have defined my connection string as both of the above which didnt work so i tried combining them to have the EF meta data (below) but the Azure connection string and that also gave me the Meta Data error:
metadata=res://*/Models.BeCivicData.csdl|res://*/Models.BeCivicData.ssdl|res://*/Models.BeCivicData.msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:becivicserver.database.windows.net,1433;Initial Catalog=BeCivic;Persist Security Info=False;User ID={myid};Password={mypass}5;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" " providerName="System.Data.EntityClient"
In my application the EF .edmx file is in a folder named Models.
Both the database element and the code elements deployed separately to azure successfully and now i just need to link them up.
Keyword Not Supported:metadata
When you attempt to use the the connectionstring within the Azure Portal Connection Strings setting, it will get the above error.
So, try to change the type from SQLAzure to Custom.
You could refer to this article to troubleshoot.
To avoid parsing the connection string yourself, you can use the EntityConnectionStringBuilder class the parse the string and retrieve the database connection string from its ProviderConnectionString property.

Entity frawork not work with azure db on azure website, but localy yes

I have problem with Entity framework. If I start web app localy thats all is right, but if I publish on azure then database not work. Localy I use same connection string to azure db, I do not have local database. Where can be problem?
<connectionStrings>
<add name="AzureDbConnectionString"
connectionString="Server=serverName.database.windows.net,1433;Database=UserName;User ID=UserName#serverName;Password=SuperPassword;Trusted_Connection=False;Encrypt=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Error: Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
Peter
On the Azure portal, go to websites -> your website -> configure. Scroll down to "connection strings" and add your connection string there. Make sure you give it the same name as in your web.config file, (in your case "AzureDbConnectionString")

The connection string in the application's configuration file does not contain the required providerName attribute

I have a WorkerRole in Azure that's connecting to an MSSQL database (also in Azure, on its own virtual machine -- in other words not an Azure SQL database). The WorkerRole is using EntityFramework (code first).
My connection string looks like this and connections from the worker role works just fine in my development environment:
<add name="MyConnectionString" connectionString="Data Source=mydatabaseserver.cloudapp.net;Initial Catalog=MyDatabase;User ID=MyUser;Password=ThePassword;" providerName="System.Data.SqlClient" />
However, the application throws the error:
The connection string MyConnectionString in the application's configuration file does not contain the required providerName attribute
As you can see, it clearly does contain the providerName attribute. I have checked the obj/Release folder to see the actual configuration file that is published to Azure, and it does contain the attribute as well.
What am I doing wrong?
Never mind. It is in fact working now.
The reason I thought it wasn't working is that there is a delay in how Azure writes Trace data to TableStorage. This led me to believe that the WorkerRole was still throwing the error after updating the connection string, even though it wasn't.

SQL Server Express and Entity Framework

I'm new to SQL Server Express (not to SQL Server in general) and I have a problem. I'm using Entity Framework (6-beta) in an ASP.NET MVC application.
I created a database using the SQL Server Management Studio. I can connect to the DB using Management Studio and my local Windows user account.
I dind't change the default configuration, so the SQL Server stores the data files in :
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA
The .mdf file of my database is stored in this folder.
My problem is: When I start the MVC application from Visual Studio, Entity Framework failes to open a connection to the database. The exception text is quite unprecise, but the inner exception reveals that it tries to connect to:
C:\Users\<MyUser>\Workspaces\<Solution>\<Project>\App_Data\<Context>.mdf
My connection string is:
"Data Source=.\SQLEXPRESS; Initial Catalog=<Name>; User Instance=False; Integrated Security=True;"
Does anyone know why Entity Framework tries to connect to this file? What can I do?
You need a web.config entry on the MVC application project (in case the DBContext is in another referenced project).
The web.config should have an entry like the one below in the connectionstrings section (note YOURCONTEXTNAME should match the name of your DBContext class).
<add name="YOURCONTEXTNAME" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YOURDATABASENAME;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
Ok. I found the problem. I edited the wrong web.config file by accident.
Asp.Net MVC 5 creates a second web.config file in the Views folder.
Of course, the connection string must be inserted in the web.config of the root folder.

Database Connection Error with ef-code-first

I'm new to ef code first and have just used the reverse engineer code first to create a model of an existing database on Microsoft SQL Server 2008.
The problem I'm having is that even though I'm providing User ID and Password in the connection string, it's giving me an authentication error while complaining about my computer name as if I were using Integrated Security (which I'm not.)
The error I get is this:
Cannot open database \"edmTestDBContext\" requested by the login. The login failed.\r\nLogin failed for user 'jwelty-thinkpad\jwelty'.
My connectionString is this:
Data Source=srv-123;Initial Catalog=edmTestDB;Persist Security Info=True;User ID=user;Password=userpass;MultipleActiveResultSets=True
It seams to me like it's ignoring my User ID and using my machine name instead.
It's interesting that the connection string was auto generated by the Entity Framework tool and it worked for building the model but not for actually connecting the model back to the source database.
Any thoughts on what's going on?
I do have full permissions with my username/password as this is what I use with Sql Server Management Studio and that's also how I created the database in the first place.
I tried adding "Integrated Security=False;" and that was no help.
It looks like EF isn't finding your connection string. Make sure that it is in the config file being used (you might need to copy it from the class library config to the application config) and that it either has the same name as the context class or that you provide DbContext with the name by calling the appropriate base constructor. For example:
public EdmTestDBContext()
: base("name=MyConnectionStringName")
{
}
There are some built-in conventions in EF Code-first such as using the name of derived context class from DbContext to find the related connection string in the .config file.
So if your context class is named BlogContext, it will look for the following connectionString first:
<connectionStrings>
<clear />
<add
name="BlogContext"
...