20

I need to set my Entity Framework connection string at runtime. Right now, I have the following:

string connectionString = "metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework"";
using (CustomerEntities entities = new CustomerEntities(connectionString))
{
  CustomerEntity entity = new CustomerEntity();
  // do more
  entities.CustomerEntities.Add(entity);
  entities.SaveChanges();
}

When I execute the code above (with the {parameter} values replaced), I get the following error:

Keyword not supported: 'data source'.

What am I doing wrong?

4 Answers 4

21

Change this.

string connectionString = "metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework"";

To this (note how i escaped the " character as "" )

string connectionString = @"metadata=res://*/DataModels.CustomerDataModel.csdl|res://*/DataModels.CustomerDataModel.ssdl|res://*/DataModels.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string= ""data source=tcp:{serverName},{portNumber};initial catalog={databaseName};user id={username};multipleactiveresultsets=True;application name=EntityFramework""";
Sign up to request clarification or add additional context in comments.

Comments

7

I know this was asked 10 months ago, but I found an easier way to specify the connectionString:

If your config file has it as:

<connectionStrings>
<add name="CustomerDataModel" connectionString="metadata=res://*/EntityFramework.CustomerDataModel.csdl|res://*/EntityFramework.CustomerDataModel.ssdl|res://*/EntityFramework.CustomerDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\CustomerDataModel;initial catalog=CustomerDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

You can specify it as -

public const string ConnectionString = @"name=CustomerDataModel";
..
CustomerDBContext context = new CustomerDBContext(ConnectionString );

No need to worry about quotes. Lot cleaner.

Comments

6

It is easier to use EntityConnectionStringBuilder and SqlConnectionStringBuilder to change parameters as you want.

Comments

3

set multiple connection strings in your web.config, and follow below:

public partial class MyDatabaseEntities
{
    public MyDatabaseEntities(string connection)
        : base(connection)
    {
    }
}

and then wherever you want to create instance of entities, pass connection string name in parameter:

MyDatabaseEntities entities = new MyDatabaseEntities("CONNECTION_STRING_NAME");

I hope this will help.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.