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.
Related
I'm developing a Windows Forms application that I need to storage some data as a database in user's system, so that I would be able to access my database through Entity Framework. I mean I need to copy a database (tables should be created already) from the resources directory to specific directory. Then use it with Entity Framework. To be honest I have no idea. That's my problem!
I make sample fixed problem for all project when create winforms & localdb
You can create 2 ConnectionString in App.config file
App.config
<add name="DevConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" /> <!-- *1* Use when publish project-->
<!--<add name="AppConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='E:\My Projects\MyDatabase.mdf';Integrated Security=True"
providerName="System.Data.SqlClient" />-->
<!--*2* Use for development-->
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
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>
Edit Question:
Sorry about this basic question. This is a new technology to me.
I am using a c#, winforms application with sqlite.
Currently I have included sqliteDb in app_data folder, Also App.Config has the connection string:
<connectionStrings>
<clear/>
<add name ="RConnString"
providerName="System.Data.Sqlite"
connectionString="Data Source= C:\project1\R1\App_Data\RVEST_V1.DB"/>
</connectionStrings>
This RconnString is being accessed in .cs file.
string connString =
ConfigurationManager.ConnectionStrings["RConnString"].ConnectionString;
When I send it to client, How would I send the database. I mean how should I change the connection string.( Instead of referring to my local C:..)
Thank you
Sun
Try using relative path like this
connectionString="Data Source=App_Data\RVEST_V1.DB"
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.