before I was using a connection string with integrated security.
Now I switched it to username/password and created a new user in my SQL server.
I generated a new connection string with the server explorer in visual studio by adding a new connection (I didnt really added a connection there, I just used the wizard and copied the connection string for the app.config).
then I had to update-database and it worked.
But now I recognized that there is no password in my new connection string.
How does it work? I can't find the password anywhere...
There are a couple ways:
1: Add the string to the constructor of your Context.
public class Context: DbContext
{
public SchoolDBContext() : base("connectionstring")
{
}
}
2: webconfig
<configuration>
<connectionStrings>
<add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
</connectionStrings>
</configuration>
As referenced from here: Entity Framework Connection String in App.Config
Related
My website, an ASP.NET MVC app, is working perfectly inside Visual Studio with database entities and such. I want to try to upload my app to GoDaddy's Plesk Web Service.
I got a guide here:
http://www.c-sharpcorner.com/article/how-to-host-your-asp-net-mvc-website-on-godaddy-server/
which says at step 19: "Write Connection String for your server side database."
and at step 20 "Open Visual Studio again and update database from package manager, again writing same connection string in web.config of your project in Visual Studio. Open package manager console and type the following command,"
Can anyone of you please give me some advice what it means by connection string?
There is connection string in web.config file.
I am using entity framework so my connection string is as per below
<add name="yourEntityName" connectionString="metadata=res:///entity.csdl|res:///entity.ssdl|res://*/entity.msl;provider=System.Data.SqlClient;provider connection string="data source=yourServerIpAddress;****initial catalog=DataBaseName;persist security info=True;user id=YourId;password=YourPassword;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
you have to change lines in your connection string where bold line in my conn. string.
I'm stymied by a problem I'm having reading a connection string for a Web API app.
I have a connection string set up in app.config as follows:
<connectionStrings>
<add name="connectionString" connectionString="Server=[server];Database=[db];User Id=user;Password=password;"/>
</connectionStrings>
This is read from code as:
string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
All works fine in development (using the proper creds of course). However when I publish, the app.config is not copied over. I can manually recreate this file on the server, but even then I get the following error:
Login failed for user '[MACHINE NAME]'.
It appears that instead of grabbing the connection string from app.config the code is instead attempting to connection to SQL Server using a trusted connection.
Note that I'm using ADO and not EF.
Any ideas?
I'm trying to get my local SQL database connecting to my ASP.Net project and nothing I try is working.
The database is named cinemaDatabase and its using windows authentication.
In web.config I have
<connectionStrings>
<add name="myDatabase" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=cinemaDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then in a cshtml page I have
#using WebMatrix.Data;
#{
var db = Database.Open("myDatabase");
}
I keep getting this error
InvalidOperationException: Connection string "myDatabase" was not
found. OpenNamedConnection
What am I doing wrong here? Thanks in advance.
Edit -
Got it working, had to make a normal SQL server instead of the local MSSQLLocalDB one and change the data source in web.config to that.
Then used this code on my page and it connects now.
#using WebMatrix.Data;
#using System.Data.SqlClient;
#{
var connectionString = "Data Source=DESKTOP-58NGNQP;Initial Catalog=webDev;Integrated Security=True";
var providerName = "System.Data.SqlClient";
var db = Database.OpenConnectionString(connectionString, providerName);
}
I believe the problem is with your connection string. Unfortunately, I don't have a running example right this moment with an attached .mdf file, but I believe something like this will do the job for you, I grabbed this string from MSDN's article on connection strings
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Couple of important facts here:
Data Source=(LocalDB)\v11.0 - This specifies the start-on-demand localdb instance to use. Note that the version, v11.0 in this string, varies from installation to installation, and you should find the correct version for your string. (You should be able to find it easier below)
AttachDbFilename=|DataDirectory|\DatabaseFilename.mdf - this tells LocalDb to load that database file. Alternatively, localdb persists database files in your user home directory, or AppData directory somewhere.
Integrated Security=true - A connection string needs an authentication scheme. Two possible options are Integrated Security, which uses your windows credentials to attempt access to your database, which works well with many things, LocalDb being one of them. Another option is to use sql authentication and a sql name and password to connect.
I have Persistence class library, which contains my DbContext class. It also contains app.config file, where I have predefined connection string like this:
<connectionStrings>
<add name="<Namespace1>.Persistence.AssessmentContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AssessmentContext.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Assuming that AssesmentContext has Namespace1.Persistence namespace.
When I try to enable-migrations using PM console, it gives me such an error:
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.
When I tried to debug it and put debug output into AssessmentContext ctor I found that the connection string is still using .\SQLEXPRESS data source.
Data Source=.\SQLEXPRESS;Initial Catalog=<Namespace1>.Persistence.AssessmentContext;Integrated Security=True;MultipleActiveResultSets=True
So what I am doing wrong? And why EF doesn't take my connection string from app.config?
The above connection string entry must be copied to the app.config of the executable program if you are using desktop app or copied to web.config if you are using web app.
We have a web service project called 'Service' and in the web.config of the 'service' I have set the connection string as follows:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=L308;Initial Catalog=Dashboard;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am trying to access the connection string from another project 'DBConnector' using the following code but getting null reference exception even though after adding the reference of the 'Service' into 'DBConnector'. using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
You will need to have the connection string in DBConnector project config file. Adding reference to a project doesn't bring in the config of that project in the main config of the project
If i got you
You can not do that if you want to use the Connection string of service in you library. You need to expose that as property or method
in you service.cs
public string connectionstring
{
get
{
return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
}
or
DBConnetor added as referance
If the DBconnector has added as referance in the Service layer than you can access it easily by the code you have written
If you need want to make that connection string Global so all you applications and services can use it you will need to put it in the machine.config.
The machine config if found here %WinDir%\Microsoft.NET\Framework\\CONFIG.
'Service' into 'DBConnector'.
Seems to be incorrect thing to do. It should have been other-way around!