I am using entity framework model first approach in WCF.
The EF creates a connection stringwhich looks something like this:
automatically for the first time.
Now I want to change the connection string as database is located in other server.
How can I do it?
Connection string looks like this for now:
<connectionStrings><add name="Entities" connectionString="metadata=res://*/Database.Model1.csdl|res://*/Database.Model1.ssdl|res://*/Database.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=AB;password=admin;persist security info=True;user id=TEST"" providerName="System.Data.EntityClient" /></connectionStrings>)
Regards
Anudeep
One simple way is to modify the app.config or web.config file with the new connection details so calling the var context = new DataContext("NewServerEntities") constructor will use the named connection in your config file.
<add name="NewServerEntities" connectionString="metadata=res://*/Database.Model1.csdl|res://*/Database.Model1.ssdl|res://*/Database.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=NewServer;password=admin;persist security info=True;user id=TEST"" providerName="System.Data.EntityClient" />
Remove the connection string from the app.config file, re-run the entity Data Model wizard by right-clicking the designer and chose Update Model From Database, it will guide you to build a new connection. Or else directly edit the Datasource and credentials in the existing connection string in App.config.
Related
I am doing a project in MVC5 EF 5 and have read a million tutorials and have come to a sudden wall.
I have my connection string that allows me to use Database fist when using Identity, and when I have to make a scaffolded Item that allows me to do CRUD, I have to switch it back to the auto created connection string.
So my question is, what can i do to get just one string to work, i cant find any documentation on what MVC5/EF is looking for when the connString is called.
here are the two string.
<!--<add name="TextronEntities" connectionString="Data Source=LEMN-476W-IT; initial catalog=Textron; integrated security=True;" providerName="System.Data.SqlClient" />-->
<add name="TextronEntities" connectionString="metadata=res://*/Models.TextronDBConnection.csdl|res://*/Models.TextronDBConnection.ssdl|res://*/Models.TextronDBConnection.msl;provider=System.Data.SqlClient;provider connection string="data source=LEMN-476W-IT;initial catalog=Textron;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
the top one is the one that lets me log in and register new users, where the bottom one is the one that lets me do CRUD.
I comment one out to login and the other to use CRUD testing.
This inst a production project but might soon be and i would like to understand everything to the best of my knowledge.
This s the connection String I am using in my local asp.net mvc site, I want to publish the website over a web hosting. They have SQL Instance of .\SQLEXPRESS12 and I have created a database name myapp_db as well. I have created a user with that db also. How can I modify the EF generated Connection String, and will it effect my auto generated models too ?
P.S. I know this question might not be StackOverflow's guidelines, but I would appreciate any help.
<connectionStrings>
<add name="DCHSurgical_DBContext" connectionString="metadata=res://*/Areas.ADMIN.Models.Model.csdl|res://*/Areas.ADMIN.Models.Model.ssdl|res://*/Areas.ADMIN.Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=myapp_db;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Just change this part of your connectionString like this:
< ... data source=Server's IP Address,Port;initial catalog=myapp_db;
user id=username that you have created;password=yourpassword;
MultipleActiveResultSets=True;App=EntityFramework""
... />
I've recently found some very old server control code and need to up and running. The problem is I don't know how to connect it to the database. Below is the SQL connection:
SqlConnection sqlConnection = new SqlConnection(this.Page.Application["AuctionDbase"].ToString());
SqlCommand sqlCommand = new SqlCommand();
StringBuilder stringBuilder = new StringBuilder();
My question is do I need a web.config file where it can grab the connection string? I've placed the database on a local SQL server but I don't know if that's the right approach. I've never worked with Server Controls before and trying to get this deployed has been difficult!
Thank you in advance!!
Are you looking at the code snippet and trying to locate where the existing connection string is?
Judging by the code snippet, it doesn't look like it's in the web.config. If it was then i would expect a call to the ConfigurationManager, not Page.Application.
Search the code to see if "AuctionDbase" is being set anywhere.
It's possible that it's being set in the global.asax file if you have one.
Another possibility is in the Project options under Settings.
There are various ways of storing/retrieving connection details.
The connectionStrings element would be the most straightforward way to handle connection strings. The connectionStrings section is dedicated to connection strings and was introduced in .NET 2.0.
If the privacy of your connection credentials is of concern: you should use the connectionStrings section, as it can also be encrypted separately from any other settings.
In the Web.config file you can add:
<configuration>
...
<configSections>
<connectionStrings>
<add name="connectionStringName" connectionString="Data Source=(local);Initial Catalog=DBNAME;User Id=XXX;Password=XXX;" providerName="System.Data.SqlClient" />
</connectionStrings>
<configSections>
...
<configuration>
for more information on connection strings you can learn more here: https://www.connectionstrings.com/sql-server/
and in your code you can get the connection string like this:
string connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
I am new to create connection string and application configuration file. I use an example which showed connection to SQL Server CE using file which is what I do not want instead I want to connect to SQL Server 2008 Standard edition.
While exploring about connection string on several links like http://www.connectionstrings.com/sql-server/ i found that the Connection string uses the Property "Data Source = " and in some places it uses "Server=" which is quiet confusing.
Here is what I have in my application configuration file.
<connectionStrings>
<add name="ShareManagement"
connectionString="Data Source=localhost"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I want someone to tell me which properties need be used and what should be their respective values. (I am using default sa user as UserID and a password and using SQL Server authentication mode. My SQL Server database is installed on same machine/server on which my Visual Studio solution / application reside).
Ragards.
You can use either server= or Data Source= (those two are equivalent), and you can use either database= or Initial Catalog= (again: those are equivalent) - take your pick, use whatever you prefer.
But you just need to define at least
server,
database,
either Integrated Security=SSPI (for integrated, Windows authentication) or User id=abc;Password=xxxx for SQL Server authentication
You need at least these three pieces of information.
So if you want to use integrated security (Windows authentication), use this connection string:
<connectionStrings>
<add name="ShareManagement"
connectionString="server=(local);database=AdventureWorks;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
but if you want to use SQL Server authorization for a user John with a password secret, use this connection string:
<connectionStrings>
<add name="ShareManagement"
connectionString="server=(local);database=AdventureWorks;User ID=John;Password=secret;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Since I'm almost exclusively using these connection strings to connect to a standard relational database server, I personally prefer to use server=.... and database=...... - those just seem more natural, clearer and more intuitive to me. But again: you can also use those other key strings - they're 100% equivalent!
In my opinion, you should use the following connection string:
<add name="MyConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
Data Source should be as of the server name, and the initial catalogue will represent the database name you have with your sql server 2008 instance. To get logged in via the integrated security can be a better choice if you have not defined a user/password separately for that particular database except 'sa' account.
I know I can create a connection string in the c# class itself, but I am trying to avoid doing that. I want to create the connection in the web.config, which I read is more secure. Nevertheless I couldn't find any example that has the following attributes specified:
Host name
Port
SID
Username
Password
Connection Name
Could anyone help please with creating this in webconfig? I am connecting to oracle DB.
Here is the template:
<connectionStrings>
<add name="{ConnectionName}"
connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;"
providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
Here is one of mine - minus a real TNS name and username and password:
<add name="MSOL" connectionString="Data Source={TNS_NAME};User ID={username};Password={password};pooling=true;min pool size=5;Max Pool Size=60" providerName="Oracle.DataAccess.Client"/>
After adding the connection string to the web.config you can use the following:
System.Configuration.ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
to retrieve the connection string.
It may can help u....
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
http://www.connectionstrings.com/oracle
You can investigate what the connection string should be like:
1) Create an empty text file in windows explorer and rename it to X.UDL
2) Double click on it and the datalink provider dialog will appear.
3) Select the provider tab. Find the provider for your data access method and click next.
4) Select your source
5) Test the connection and save it.
6) Compare the contents of X.UDL with your connections string.