Wednesday, May 26, 2010

ADO.Net Entity Framework Connection String: "Keyword not supported: 'Data Source'"

So you're new to ADO.Net Entity Framework, and you build a lovely little app, and you use your old connection string from a pre-linq application or website.  You put your connection string in the app.config / web.config, which most likely looks something like this:

data source=ServerName;Initial Catalog=SomeDatabase;User Id=sa;Password=pwd

You reference this connection string in your code like this:

using (var ents = new LinqADOEntities(Settings.Default.ConnectionString))  {



And yet when you fire it up, .NET freaks out!




Well that's no good.  There are countless posts / forums / blogs on the web around this, most of which are confusing, wrong or otherwise leading you down the primrose path.

Here's the real skinny:

If you're using ADO.Net Entity Framework, it generates its OWN connection string directly into your app.config / web.config, and it looks NOTHING like an old connection string.



<connectionStrings>
<add name="STS_Content_TFSEntities" connectionString="metadata=res://*/TFS2005_Documents.csdl|res://*/TFS2005_Documents.ssdl|res://*/TFS2005_Documents.msl;provider=System.Data.SqlClient;provider connection string="Data Source=someServer;Initial Catalog=SomeDB;User Id=sa;Password=pwd;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>




WTF?!  Yeah.  So if you want to store this in another place, then copy the whole wacky thing over to the other place, wierd metadata= stuff and all.  Otherwise, you'll need to change the values in the app.config / web.config to match your database configuration and call your ADO Entity with an empty constructor.  It will find the default connection string as defined in the App.config / web.config.  As in...

using (var ents = new LinqADOEntities())  {




If I've saved anyone the 1.25 hours it cost me, I'll consider it worth my while.



No comments:

Post a Comment