How do I change this code so that I can dynamically create the connection string instead of just using the one in my web.config file? I have the code for building a connection string but don't know if I can just insert it into the constructor here.
public MyEntities()
: base("name=MyEntities")<---Do I need to remove this?
{
string providerName = "System.Data.SqlClient";
string serverName = "server";
string databaseName = "database";
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;
string providerString = sqlBuilder.ToString();
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = providerString;
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = @"res://*/Models.CurrentData.csdl|res://*/Models.CurrentData.ssdl|res://*/Models.CurrentData.msl";
using (EntityConnection conn =
new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
}
}
What do I write/change after this to create the connection. Is there any other method I need to change?
I updated my first post to give you a clearer picture of what I need. I don't know what the database name or servername when a client goes to the website. I have another database that will keep track of server & databasename where the client's info is stored
so when the client comes to the website, I look at the hostname they used and check the database. Once I find that information, I need to create a connection string from the info in the database.
I'm getting this error when trying to use the connection string that I made. I put in values for server name and database name.
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection
string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code
that throws this exception.
I haven't used database first in a while, so someone else more knowledgeable might have a better answer, but based on this note in the Connections doc:
You can use an existing DbConnection object by passing it to a DbContext constructor. If the connection object is an instance of EntityConnection, then the model specified in the connection will be used rather than calculating a model using Code First. If
the object is an instance of some other type—for example, SqlConnection—then the context will use it for Code First mode.
I would try creating the EntityConnection object outside of the DbContext class and pass it in to the DbContext constructor.
Thanks for trying. I was passing in the connection string before when I knew what it was going to be. Now I want to change it so that I am able to dynamically create connection strings.
base("name=MyEntities")<---Do I need to remove this?
Hi,
In my mind, I don't suggest you doing so because this will let you read in from web.config, and you can dynamically change the connection string in the web.config. The best nice thing is that you can dyanmically change the string value in the web.config
without re-building the whole app project.
frogo
0 Points
7 Posts
Quick question on dynamic connection string for entity framework.
Jan 31, 2013 06:15 PM|LINK
How do I change this code so that I can dynamically create the connection string instead of just using the one in my web.config file? I have the code for building a connection string but don't know if I can just insert it into the constructor here.
public MyEntities() : base("name=MyEntities")<---Do I need to remove this? { string providerName = "System.Data.SqlClient"; string serverName = "server"; string databaseName = "database"; SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = serverName; sqlBuilder.InitialCatalog = databaseName; sqlBuilder.IntegratedSecurity = true; string providerString = sqlBuilder.ToString(); EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); entityBuilder.Provider = providerString; entityBuilder.ProviderConnectionString = sqlBuilder.ToString(); entityBuilder.Metadata = @"res://*/Models.CurrentData.csdl|res://*/Models.CurrentData.ssdl|res://*/Models.CurrentData.msl"; using (EntityConnection conn = new EntityConnection(entityBuilder.ToString())) { conn.Open(); } }tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 06:27 PM|LINK
You can use the full connection string in the constructor -- see Other DbContext constructor options in http://msdn.microsoft.com/en-us/data/jj592674
frogo
0 Points
7 Posts
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 06:32 PM|LINK
I updated my first post to give you a clearer picture of what I need. I don't know what the database name or servername when a client goes to the website. I have another database that will keep track of server & databasename where the client's info is stored so when the client comes to the website, I look at the hostname they used and check the database. Once I find that information, I need to create a connection string from the info in the database.
frogo
0 Points
7 Posts
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 06:40 PM|LINK
Also am I able to do this even when I'm using a database first method.
frogo
0 Points
7 Posts
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 06:50 PM|LINK
I'm getting this error when trying to use the connection string that I made. I put in values for server name and database name.
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
My project is database first.
tdykstra
Contributor
4523 Points
630 Posts
Microsoft
Moderator
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 07:04 PM|LINK
I haven't used database first in a while, so someone else more knowledgeable might have a better answer, but based on this note in the Connections doc:
You can use an existing DbConnection object by passing it to a DbContext constructor. If the connection object is an instance of EntityConnection, then the model specified in the connection will be used rather than calculating a model using Code First. If the object is an instance of some other type—for example, SqlConnection—then the context will use it for Code First mode.
I would try creating the EntityConnection object outside of the DbContext class and pass it in to the DbContext constructor.
frogo
0 Points
7 Posts
Re: Quick question on dynamic connection string for entity framework.
Jan 31, 2013 07:07 PM|LINK
Thanks for trying. I was passing in the connection string before when I knew what it was going to be. Now I want to change it so that I am able to dynamically create connection strings.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Quick question on dynamic connection string for entity framework.
Feb 02, 2013 02:12 AM|LINK
Hi,
In my mind, I don't suggest you doing so because this will let you read in from web.config, and you can dynamically change the connection string in the web.config. The best nice thing is that you can dyanmically change the string value in the web.config without re-building the whole app project.