So have just downloaded the source code for a new project that builds and runs fine on a couple other developer's boxes. I am getting the error:
The ConnectionString property has not been initialized.
I am able to connect to the database via SQL Server Management Studio using the connection string in my Web.config without issue.
The source code for the library that is throwing this error is not available.
The project is an ASP.NET MVC Project with the following values in the Web.config
<connectionStrings>
<add name="DbConnection" connectionString="Data Source=la-foo-server\development,1444;database=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="ConnectionStringName" value="DbConnection" />
</appSettings>
I have tried adding an App.Config to the bin and root of my ASP.NET Website just in case, for some reason, the lib with out source code, is trying to read an App.Config and is ignoring the Web.config.
StackOverflow solutions that have not helped:
How to fix "The ConnectionString property has not been initialized"
The ConnectionString property has not been initialized error
Solve the error "The ConnectionString property has not been initialized."?
A developer had this same situation on a Windows 8.1 environment the other day, he moved to a Windows 7 environment and the error was fixed. I am using Windows 7 pro so I wonder if it is an issue with my .NET Frameworks installations.
The ASP.NET MVC Website is targeting version 4.0. Most of my other projects that all work without issue are targeting .NET 4.5
So I was able to get the source code for the class library that loads the connection string and it turns out the code is loading a collection of connectionstrings like so:
var collection = System.Web.Configuration.WebConfigurationManager.ConnectionStrings ?? System.Configuration.ConfigurationManager.ConnectionStrings;
if (collection[1].Name == null)
throw new Exception("Unable to detect connection string in app.config or web.config! Default connection string name is \'DbConnection.\'");
db = new Database(collection[1].Name);
My machine.config happened to have a connection string in it for MySql, called LocalMySqlServer. This connection string may have been added by the .NET MySql Connector installation or I may have made the change myself.
So the connectionstring collection was loading a connection from my machine.config, so collection[1] array position did not have the expected connection string in it. This explains why some developers had no issue on their machines and other developers did have issues on their machine.
Option 1:
<connectionStrings>
<add name="DbConnection" connectionString="Data Source=la-foo-server\development,1444;Initial Catalog=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
Option 2:
<connectionStrings>
<add name="DbConnection" connectionString="Data Source=la-foo-server\development;Initial Catalog=mydb;user id=myId;password=myPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>
Related
I use Entity Framework 6 code-first in a Windows application to connect to a SQL Server database. How could I edit my code to enable me or any app user to change the database name without getting any errors?
You can set the connection string separately in the configuration file and then use ConfigurationManager class to read strings.
But to change only the database name without other errors, you must ensure that other settings are the same. Otherwise, you must modify the corresponding code or settings according to the actual situation.
For example: add in the configuration file (config)
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then call it like this:
string connectionString = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
For detailed examples, please refer to documentation.
I'm using EF for my LiveCharts inside a user control but I'm getting an error
No connection string named 'KiculoServerEntities' could be found in the application config file.
I managed to fix it once after I deleted and create a new EF but after I restarted VS this error showed again,
<add name="KiculoServerEntities"
connectionString="metadata=res://*/KiculoCraftModel.csdl|res://*/KiculoCraftModel.ssdl|res://*/KiculoCraftModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-1281SGB;initial catalog=KiculoServer;persist security info=True;user id=KicuCrafts;password=admin;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
Be sure to put that line under:
<configuration>
<connectionStrings>
<!-- here put your connection string -->
</connectionStrings>
...
</configuration>
Connection String Must be available in Caller Project for e.g. If you have Project A and B. Where A is UI and B is EF project, make sure you have Connection String in Project A too
Or if you can shared Context Class and How did u call it in a project like using Startup.cs
I'am getting an error in my console application program and it says :
No connection string named 'SimpleBankEntities' could be found in the application config file.
However, I added entityFramework as a Reference. Also I have everything in my app.config file. Here is my <connectionStrings> :
<connectionStrings>
<add name="SimpleBankEntities" connectionString="metadata=res://*/SimpleBankModel.csdl|res://*/SimpleBankModel.ssdl|res://*/SimpleBankModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Training;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Thanks a lot, have a nice day !
If you need something more than that, please let me know.
Do you have more than one project in your solution? If so, did you add the connection string to the config file in your startup project?
Everything looks correct if this is the configuration file in the start up project.
Sorry if this has been asked, but I've gone through all of the other posts I can find and everyone seems to be using an EDMX, and not a "production" database.
I get the following error:
System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource
when attempting to access my application. My connection string is as follows:
<add name="FormAssemblyEntities" connectionString="metadata=res://*/Models.Entities.FormAssembly.csdl|res://*/Models.Entities.FormAssembly.ssdl|res://*/Models.Entities.FormAssembly.msl;provider=System.Data.SqlClient;provider connection string="data source=server\instance;initial catalog=dbname;user id=userid;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
This works perfectly if I am running it locally and it connects to the edmx with the following connection string, but as soon as I put it on our UAT server and point it to the database, I get that error.
<add name="FormAssemblyEntities" connectionString="Data Source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\FormAssembly.mdf;Initial Catalog=FormAssembly;Integrated Security=SSPI;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
I really have no idea where to start. The database appears up to date, the user has access, and the server has access to the SQL server.
I have also tried replacing the res://*/ with res://FormAssembly/ and res://FormAssembly.dll/, but the former results in the same error, and the latter says the dll can't be found (the DLL is in the bin directory).
Any help would be appreciated.
Thanks,
Travis
In my case, the metadata part of my connection string was malformed.
The following connection string worked in my case:
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=res://*/Northwind.csdl|res://*/Northwind.ssdl|res://*/Northwind.msl;provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=Northwind;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
I'm trying to connect my database from my SQL Server to my asp.net project through the web.config file. I found code like this which should be able to connect my SQL Server and ASP.net project together properly. However after adding this code, I got this error.
This is my SQL Server connection string that is supposed to be place in web.config:
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=sergio-desktop\sqlexpress;Initial
Catalog=MyDatabase;User ID=userName;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
And this is the error
I'm also not very sure if the syntax of my connection string to establish connection between the two is correct.
As the error suggests, the connection element should not go in system.web section. it should go in configuration section of your web.config
as
<configuration>
<connectionStrings>
---