We have an ASP .NET (MVC) app and are using Entity Framework 6 to connect to our databases. The DbContext is constructed in a standard way and it loads the connection string on our behalf. The generated code looks like this:
public partial class MyContext : DbContext
{
public MyContext(string connectionName)
: base("name=" + connectionName)
{
}
}
We set the connection string in a local web.config also in a standard way:
<configuration>
<connectionStrings>
<add name="DefaultConnection"
connectionString="metadata=...;provider connection string="...""
providerName="System.Data.EntityClient" />
When we publish the app to Azure we navigate to the Azure Portal, then to the Web App's Settings, then to the list of Connection Strings. There we add the EF connection string that we had used locally. When we restart and visit the app we get a run-time error depending on the type of connection string we choose.
For a Custom type we get the following run-time error:
Keyword not supported: 'data source'.
For SQL Server or SQL Database we get the following run-time error:
Keyword not supported: 'metadata'.
This really seems like a straightforward story so we are wondering what is going wrong.
The problem is the escaped quotes: ".
The connection strings in web.config have quotes escaped because they are serialized in an XML attribute. When entering a connection string in the Azure portal you should provide the raw unescaped string. Something like this:
metadata=...;provider connection string="Data Source=..."
David Ebbo's answer is good for confirming that the Environment is set up as you expect. It is also helpful to pay attention to the .pubxml file when publishing via the wizard in Visual Studio: it will try to populate connection strings as well.
'custom' should be correct here. In that case, the providerName is left unchanged, so if you have System.Data.EntityClient in your config, that should remain after the Azure runtime changes it.
Try going to Kudu Console and click on Environment to make sure the conn string looks correct there.
If you have this line in web.connfig
<add name="Entities" connectionString="metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"" providerName="System.Data.EntityClient" />
Add this in azure portal:
Name Column => Entities
Value Column => metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX"
"Custom" - In the drop selection box
Make sure (as stated in first answer) to replace " with "
Just recording my own experiences in addition to answers already recorded here
This was my final connection string (on mutliple lines for clarity)
metadata=res://*/Models.mBT.csdl|res://*/Models.mBT.ssdl|res://*/Models.mBT.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=tcp:myazureserver.database.windows.net,1433;
Initial Catalog=databasename;
User ID=z#myazureserver;
Password=xyz"
To convert from a "normal" connection string to one that is accepted by EF:
The connection string type in the application settings has to be "other" not "SQL Azure"
The connection string value automatically replaces anything published in web.config
The metadata name Models.mBT.csdl (and the other two) comes from this:
Firstly, mBT is the name of my .edmx file
With regards to the Model. bit, see the answer from #leqid here: MetadataException: Unable to load the specified metadata resource
You can inspect your obj directory and see these three metadata files are in a subfolder called Models, so you need to prepend with Models.
Related
I added all my connection strings to the App Service -> Settings screen.
I'm using Redis to share Sessions between the different instances. Everything is good if I have the connection in the ConnectionString section in the web.config but if I remove it from there and just add it to the Connection string section under the settings screen then it all breaks.
I'm running a classic .net web application on .net 4.7
I've tried the following so far:
Added a connection string in web.config without a value hoping it will overwrite it
Completely removed the connection strings in the web.config file.
Just for a test, I added an extra connection string which I can later retrieve and show on a web page.
How early do the connection strings from azure get applied? Is there a workaround or is this a known issue (haven't managed to find anything about it)?
EDIT: Here is a sample of what I'm trying to do.
I have a connection string called <add name="redis.sessions" value=""/> and a session state provider that points to that connection string
<add name="redis" type="Sitecore.SessionProvider.Redis.RedisSessionStateProvider, Sitecore.SessionProvider.Redis" applicationName="private" connectionString="redis.sessions" pollingInterval="2"/> and in the session provider on Initialization it tries to initialise the Redis connection with a blank connection string. Even through one has been provided in the App Service Settings.
Cannot read connection string from web API configuration when debugging in Visual studio 2010
I am writing an ASP.net web API application and this is my first one. I am trying to set up my connection string and retrieve it using the configuration manager. There are three configuration files, web.config, web.debug.config and web.release.config. In each of these files I have added my desired connection string and have removed the default connection strings. But configuration manager still pulls up a default database connection string from someplace. Doing a global search through all the files in the solution there is no indication where configuration manager is reading this default string. How can I get my application to read the connection string that I specify in the configuration files? Why is configuration manager not reading any of these configuration files?
This is the string that I get back from configuration manager that I have no idea where it is getting it:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I am attempting to read the configuration string like this:
string CONNECT_STRING = ConfigurationManager.ConnectionStrings["Jobs"].ConnectionString;
Try using this connection string
<connectionStrings>
<add name="Sqlcnn" providerName="SqlClient" connectionString="Data Source=Datasource name; user id=userid; password=pwd; database=DbName" />
</connectionStrings>
Try using the below connection. Other case the connection you are using ./SQLEXPRESS might not exist.
When I encountered this problem I was running unit tests. For some reason when running unit tests the web API application does not read the connection string from the web config file. However, if I call the web API application from my client application the configuration string is read from the configuration file. I'm not exactly sure why the configuration file is not read during unit tests, but I'm sure I will figure it out in time.
I use ASP.NET for web and Oracle for the database. For communicating between ASP.NET and Oracle I use ODP.NET. I have specified the connection string in web.config file as such:
<connectionStrings>
<add name="main" connectionString="Data Source=AGAPUSTEST; User Id=agapus;Password=pswd;"/>
</connectionStrings>
When I try to establish connection using this connection string I get "Integrated Security is an invalid connection string attribute" error. As you can see I have not specified this attribute in the configuration file. So at some point this attribute probably gets added automatically.
I used to have the connection string hard coded and I didn't have any problems at all. Do you have any ideas how to solve this?
Luckily I have found the solution. I'd provided the web config content but I had not given you the code I use to access the connection string from code behind. Here's the code:
string conString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
As I was suspecting Integrated Security attribute being added automatically I was almost sure that some other connection string was retrieved. I tired changing ConnectionStrings[0] to ConnectionStrings["main"] and voila, it worked. So although there's only one connection string in the configuration file, the 0th one is not the one I needed. When I displayed the 0th connection string look what I got:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I'm sure you guessed where it comes from, from the machine.config file. So to make everything clean and safe I've once more been convinced that accessing by name is most of the time a better idea.
Looks like they are talking about this problem here:
https://community.oracle.com/thread/585813?tstart=405
Seems the solution could be to use User Id=/ in the connection string and see if that works
Also make sure to have your sqlnet.ora have "SQLNET.AUTHENTICATION_SERVICES = (NTS)"
I'm currently working on an ASP.NET MVC 4 project that is using Entity Framework 4.3 with a CodeFirst approach using simple POCO classes. Also, I'm using SQL Server Express as the development database.
After building my classes and setting the connection string, I ran my project and it generated a SQL Server Express database for me with no problems.
The problem though, is that I am trying to deploy to AppHarbor and I'm having an issue with the connection string. AppHarbor requires that you install SQL Server as an 'Add-On' and configure the connection string to have an alias that will inject their Sequilizer connection string into the project that you push from GitHub.
Here is their documentation on how this works: http://support.appharbor.com/kb/add-ons/using-sequelizer
I believe I have all of this setup correctly, but there seems to be a problem with how my app is reading the connection string.
Here is the development connection string that I am using on my local machine:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-FranchiseManager-201275154247;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Here is what the AppHarbor Sequilizer connection string looks like:
<connectionStrings>
<add
name="DefaultConnection"
connectionString="Server=0691298f-d461-40cc-86d6-a089016ba88d.sqlserver.sequelizer.com;Database=<removed hash value>;User ID=<removed hash value>;Password=<removed hash value>;"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
The first connection - generated locally by my EF - works just fine. The second - created by Sequilizer - is not being read by my application.
However - I can connect to the Sequilizer database through SQL Server Management Studio. So it must be my app right?
In order to trouble shoot my deployment to AppHarbor, I hard-coded their connection string into my app instead of the one auto-generated by EF and tested on my local machine.
Here's what I did:
Replaced the connection string in Web.config with the one AppHarbor
provided,
Cleaned the solution,
Rebuilt the solution
But when I ran the application, it is still utilizing the original database generated by EF - which must mean that it is still reading the old connection string.
It seems like changing the connection string is not enough. What else in my application should I change in order to replace the connection string?
Any advice is appreciated - thanks!
EDIT
Here is my DbContext class:
public class FranchiseManagerContext : DbContext
{
public DbSet<FranchiseSet> FranchiseSets { get; set; }
public DbSet<Franchise> Franchises { get; set; }
}
This works as-is with the connection string named "DefaultConnection".
How does EF know to match the DbContext with the connection string in this scenario, but it cannot do it if you change the name of the string?
UPDATE
I think I know what it is now from this SO answer: What is the point of "Initial Catalog" in a SQL Server connection string?
It looks like the Initial Catalog attribute is specifying the particular database to be used when Entity Framework first kicked in.
You have to specify the name of the connectionstring you want your DbContext to load. It has no way to magically guess that you want it to use the one called DefaultConnection. There's a heuristic that says that if no name is specified, it'll look for for a connectionstring with name set to the name of the class that inherits from DbContext. Ie. if you have:
MyAwesomeDatebase : DbContext
... then Entity Framework will work out of the box with this:
<add name="MyAwesomeDatebase" connectionString="blah" providerName="System.Data.SqlClient" />
... but if you have:
<add name="DefaultConnection" connectionString="blah" providerName="System.Data.SqlClient" />
... then it won't work because Entity Framework has no way of knowing that MyAwesomeDatebase goes with DefaultConnection.
To make that work, do this:
public class MyAwesomeDatebase : DbContext
{
public MyAwesomeDatebase() : base("DefaultConnection")
}
... and you're golden.
Ok, I think I've finally got this working.
I can't find this anywhere in the AppHarbor documentation (perhaps it is something specific to .net mvc?), but the connection string must have the same name as my DbContext class.
I found some hints on this by looking at some other questions on SO.
For anyone else that may run into the same issue:
be sure the name attribute of the ConnectionString in Web.config is the same name as your DbContext Class
Commit and push the project to your repo
Make sure the Alias name on AppHarbor is the same name as your connection string
Deploy the new build on app harbor after step 3 (this is necessary to pick up the new connection string name)
After doing that my app picked up the Sequilizer database and it's now working fine.
private string conString = "Data Source=173.56.33.6;Database=musicapp;User ID=guest;Password=guest";
I was working on local database at that time my application was successfully interacting with mysql database.when put the database on server, my application still taking the old connection string and data is stored in local database and not on server.
what is wrong?
I'd remove hard coded connection strings all together. There is a dedicated section of your config file for this very purpose:
<connectionStrings>
<add name="MusicApp" connectionString="Data Source=173.56.33.6;Database=musicapp;User ID=guest;Password=guest;" />
</connectionStrings>
Which you can then read out:
string connection = ConfigurationManager.ConnectionStrings["MusicApp"].ConnectionString;
I think your problem is that you have the connection string hard-coded in your code (as a private string that you show above). A much better way is to store it in the config file, use Settings in VS and select ConnectionString as type.
Make sure whether you updated your connection string when you transfered your DB to server. In any case it is best to store connection string in web.config, so that you can modify it when ever your db is changed or transferred to another location. This change in connection string in your web.config wouldn't require you to rebuild your application. Although if your connection string is hard-coded in code, then you would require to rebuild your application when ever you change the connection string.
if Data Source=173.56.33.6; is the location of your server database try this instead Data Source=\173.56.33.6;