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
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'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.
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>
THIS CONNECTION STRING WORKS
On my Local system i was using the database connection string
<connectionStrings>
<add name="GaugeSelectorEntities" connectionString="metadata=res://*/Gage.csdl|res://*/Gage.ssdl|res://*/Gage.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GEORGE1\SQL08R2;Initial Catalog=GaugeSelector;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
This works perfectly fine.
CHANGED TO ATTACH DB FILE INSTEAD AND IT THROWS ERROR
Now i have to deploy the WPF application and so i want to attach the mdf file from the database. So as suggested from existing resources on web i change the string to
<connectionStrings>
<add name="GaugeSelectorEntities" connectionString="metadata=res://*/Gage.csdl|res://*/Gage.ssdl|res://*/Gage.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GEORGE1\SQL08R2;AttachDbFilename=|DataDirectory|\DB\GaugeSelector.mdf;Initial Catalog=GaugeSelector;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
The only change being
AttachDbFilename=|DataDirectory|\DB\GaugeSelector.mdf;
I infact copied the mdf file of database from it's location to a folder DB in the executable path. By the way i was using LINQ to EF. But now i start getting the error.
{"The underlying provider failed on Open."}
Now i read somewehere it might happen if object context is not opened. So i put
GSData.Connection.Open();
But now it breaks here with the same error.
Is my approach itself wrong (Then what should be the approach) or do i just need some minor corrections on the connection string?
The C# project I'm working on uses nHibernate and the connection string is in the web.config as a property of a Hibernate element. I need to read the connection string in the installer to get a connection manually without using Hibernate. I know I can use configManager.connectionStrings, but as the connection string is already defined in the Hibernate portion of web.config I don't want to copy it again into the connectionStrings element. So how can I access this?
You could put the connection string in the <connectionStrings /> section of the web.config and then have NHibernate get it from there. In the NHibernate settings, remove the <connection.connection_string> property and replace it with <connection.connection_string_name> supplying the name from the <connectionStrings> section. See here for details.
<hibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string" value="${local}"/>
</hibernate>
<connectionStrings>
<add name="local" connectionString="server=(local);database=db;Uid=username;Pwd=password;"/>
</connectionStrings>
This makes it available in your ConfigurationManager, but only referenced once.