Bill Jones
Bill Jones

Reputation: 701

Setting Entity Framework Connection String at Runtime Throws "Keyword Not Supported"

I'm trying to set my connection string in my Entity Framework at runtime. In an attempt to do this, I have the following code:

string connectionString = "tcp:[serverName],[port];initial catalog=[databaseName];user id=[userName];password=[pwd];MultipleActiveResultSets=True;App=EntityFramework";
using (MyEntities entities = new MyEntities(connectionString))
{
  MyEntity entity = new MyEntity();
  // Set entity properties here

  entities.MyEntities.Add(entity);
  entities.SaveChanges();
}

When I get to the line that says entities.MyEntities.Add(entity), an exception is thrown. That exception has the following information:

Type: ArgumentException
Message: Keyword not supported: 'tcp:[serverName],[port];initial catalog'.

Why won't the code that I've written work? For the life of me, I can't figure it out.

Thank you!

Upvotes: 1

Views: 790

Answers (1)

Bennor McCarthy
Bennor McCarthy

Reputation: 11675

You're missing "Server":

string connectionString = "Server=tcp:[serverName],[port];initial catalog=[databaseName];user id=[userName];password=[pwd];MultipleActiveResultSets=True;App=EntityFramework";

(Assuming you are just substituting [serverName], [port], [databaseName] into the code in your question to hide the sensitive details. Obviously [serverName] isn't a valid hostname.)

I'm not 100% sure that "Server" and "Initial Catalog" work together, so you may need "Data Source" instead of "Server", or "Database" instead of "Initial Catalog".

Upvotes: 2

Related Questions