I actually deleted the previous connection and the models from my project and added them again. However, now I can't connect to the database again. I verified the connection strings in all the web.config and app.config, but they are the same.
Web.config:
<add name="AuthenticationAppEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyPC;Initial Catalog=AuthenticationApp;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework""
providerName="System.Data.EntityClient" />
Another web.config:
<add name="AuthenticationAppEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyPC;Initial Catalog=AuthenticationApp;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework""
providerName="System.Data.EntityClient" />
App.config:
<add name="AuthenticationAppEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyPC;Initial Catalog=AuthenticationApp;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework""
providerName="System.Data.EntityClient" />
I have no idea what could be wrong there. Any help?
When I enable Common Language Runtime Exception, I get this error :
'(db.Table).Local' threw an exception of type 'System.InvalidOperationException'
check these links, it may help you.
http://forums.asp.net/t/1749216.aspx?Code+First+Entity+Framework+not+creating+tables+in+the+database
http://www.codeproject.com/Questions/299466/Code-First-Entity-Framework-not-creating-tables-in
How to handle System.InvalidOperationException in entity framework?
I am using connection string something like below
<add name="AuthenticationAppEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyPC;Initial Catalog=AuthenticationApp;User ID=xyz;Password=Password#1;MultipleActiveResultSets=True; App=EntityFramework"" providerName="System.Data.EntityClient" />
Related
I have problem when i try to connect to localdb from visual studio 15.
I have installed SQL Server Express 2016
I folowing this tutorial: http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string
I create model and context(MovieDBContext) class and try to setup connection:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/>
When i try to access to page where i show all movies :
public ActionResult Index()
{
return View(db.Movies.ToList());
}
I get this exception:
I try to edit connection strings like this and also not working:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/> </connectionStrings>
I get this excpetion when i use (local)MSSQLLocalDB
What i do wrong ?
The initial Catalog is missing in MovieDBContext connection string.
It needs to be as follows:
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Initial Catalog=Movies;Integrated Security=True" providerName="System.Data.SqlClient"/>
In my project, I have upgrade some of the business to DbContext, previous version was on ObjectContext.
I want to have one ConnectionString in web.config for both DbContext and ObjectContext.
<add name="OjContext" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=server-pc;Initial Catalog=db;User ID=sa;password=123;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
<add name="DbContext" connectionString="Data Source=server-pc;Initial Catalog=db;User ID=sa;password=123;" providerName="System.Data.SqlClient" />
You'll want to build your "OjContext" connection object in your code, using the "DbContext" connection string for EntityConnectionStringBuilder.ProviderConnectionString property.
See more here: https://msdn.microsoft.com/en-us/library/vstudio/Bb738533(v=VS.100).aspx
How to add host's name into connection string?
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Mapping.WksModels.csdl|res://*/Mapping.WksModels.ssdl|res://*/Mapping.WksModels.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=MyDb;password=test;persist security info=True;user id=test"" providerName="System.Data.EntityClient" />
</connectionStrings>
MyDb in Data source=MyDb should be the host name (not the db name)
Here's what worked for me:
string conn = "DATA SOURCE=YOURhostname.com:1521/DBNAME;USER ID=YOURUSERNAME;PASSWORD=YOURPASSWORD";
Do not add any additional spaces or quotes. Good luck.
I have been testing out different things and have concluded that the following error is the reason my database will not initialize and throws an exception. The exception occurs when I reference the database and initialize it. My Web.config connection string is fine as you can see. I am using Entity Framework 6, MSSQL, and C#. Any help would be appreciated.
Exception:
Cannot open database \"xxx\" requested by the login. The login failed.\r\nLogin failed for user 'xxxx'.
Connection String:
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
Code:
private SchoolContext db = new SchoolContext();
public ViewMethod Method()
{
var students = from s in db.Students select s; //EXCEPTION IS THROWN HERE
}
You should mention userid and password in connection string
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;User ID=xyz;pwd=top$secret;" providerName="System.Data.SqlClient"/>
Your connection string is not in the format required by EF, it should actually look similiar to this:
<add name="XYZ" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Also use linq query.
var students = db.Students;
<connectionStrings>
<add name="WebDBContext" connectionString="metadata=res://*/Models.ModelName.csdl|res://*/Models.ModelName.ssdl|res://*/Models.ModelName.msl;provider=System.Data.SqlClient;provider connection string="data source=servername;initial catalog=databasename;user id=username;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
replace the ModelName to your Model and change connections on your db.
I decided to transfer my project from work to home and I am having some trouble with the connection to the database. This one works on work:
web.config:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=XXXXXX\SQLSERVER2008;Persist Security Info=true;Initial Catalog=esResearch;User ID=XXXXXX; Password=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchConnectionString" connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
app.config:
<connectionStrings>
<add name="esResearchModels.Properties.Settings.esResearchConnectionString"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchModels.Properties.Settings.esResearchConnectionString1"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchModels.Properties.Settings.esResearchConnectionString2"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
I guess I do not need all those strings but it works atleast. And this line is used in designer.cs
base(global::esResearchModels.Properties.Settings.Default.esResearchConnectionString2, mappingSource)
I have done the movie sample project on asp.net/mvc and used this connectionstring and this one works on my computer at home.
Web.config:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;database=Movies;User ID=sa;password="
providerName="System.Data.SqlClient"/>
</connectionStrings>
Any ideas?
There are so many different connection strings are available:
General(Windows Authentication):
SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=database;Integrated Security="True");
(SqlServer Authentication):
SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Uid=sa;password=sqlserver;database=databasename");
If you want to know more about connection string, go to:
http://www.connectionstrings.com