I’m dealing with a C# application using EntityFramework and an object derived from DbContext.
I need to set programmatically the connection string when a new instance of MyDbContext object is created. In order to do so I use the following code:
public MyDbContext(string myString)
{
this.Database.Connection.ConnectionString = myString
}
myString = "Server=MYSERVER\\SQLEXPRESS; Database=MyDB; User ID=user; Password=pass;";
or
myString = "Data Source=|DataDirectory|\\AirecCalcDatabase.sdf";
The database providers are different for the two strings. SQLExpress for the first one, SQLCompact for the second one.
If I try to run this code with the first string, I get ArgumentException coming from the connection string. For example “Keyword not valid: server” but also “Keyword not valid: database”. The connection string works when using the DbContext with no arguments, which is, reading the string from app.config
When you’re wanting to use CE, you may need to change the DefaultConnectionFactory to point to SQL CE:
See this blog post for more information