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
Related
I want to create a new database using EF6. This is my connection string. I'm using code-first.
<connectionStrings>
<add name="local"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
After running my asp.net app EF completely creates database, but it's name changes to SAMPLEDBNAME_349a392a7d84452287b4948d4f2ab5cf (it's probably a guid).
What do I have to do to create database with name specified in connection string?
Add this to your connection string:
Database=DatabaseName
or InitialCatalog=DatabaseName
like so(if registering with dependency injection):
string connection = #"Server=server_address;Database=databaseName;PersistSecurityInfo=False;User=username;Password=pa$$;MultipleActiveResultSets=False;";
services.AddDbContext<DbContextName>(options => options.UseSqlServer(connection));
Or in your case
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;Database=DatabaseName;MultipleActiveResultSets=True" />
or if Database=DatabaseName won't work
try InitialCatalog=DatabaseName
I have two connections in the web.config basically they are calling the same database. I want to manage this in a better manner because change in one config also needs to change the second connection string as well.
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;persist security info=True;user id=test;password=test123;database=db-AUTH" providerName="System.Data.SqlClient" />
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=db-AUTH;persist security info=True;user id=test;password=test123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, is there any way i can call the connection string into the other connection string.
For example. default connection connection string into db entities something like
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string= DefaultConnection" providerName="System.Data.EntityClient" />
Any suggestions would be appreciated thanks.
You are not obliged to use connection string defined inside app.config(web.config) file for entity connection. You can change entity connection string at runtime. Read this article for that: http://www.c-sharpcorner.com/UploadFile/dacca2/pass-connection-string-in-run-time-to-entity-framework/ .
Also you can get another connnection string and separete every part of connection string( DataBase,DataSource and etc.) using StringConnectionBuilder class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx.
The solution is: Get DefaultConnection string and change entity connection at runtime.
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" />
I have an ArgumentException : Keyword not supported: 'metadata'.
I tried many things, read many posts. I dont know what to do...
This is my CS :
<add name="CDPContext"
connectionString="metadata=res://*/CDP_Model.csdl|
res://*/CDP_Model.ssdl|
res://*/CDP_Model.msl;
provider=System.Data.SqlClient
provider connection string='Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;
Integrated Security=True;
MultipleActiveResultSets=True;
App=EntityFramework'"
providerName="System.Data.SqlClient" />
Thanks.
the connection string you provided above is for model first.
to use connection string for code first you could write your connection string as below
<add name="CDPContext" connectionString="Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
and also go to you context, if there is a throw exception in it, just remove it.
providerName="System.Data.SqlClient" is the wrong provider for DB First
instead use providerName="System.Data.EntityClient" to access the db using the EDM...
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.