The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,497 other followers

Don’t sleep when you use your Entity Framework model from a separate assembly

Posted by jpluimers on 2011/05/05

The Entity Framework needs you to have a connection string in your App.Config.

It is good practice having your Entity Framework model in a separate assembly.

When using it in your main app, be aware that the connection string that belongs to your model needs to be in the App.config of your main app.

If not, you get an exception like this:

System.ArgumentException was unhandled
Message=The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Source=System.Data.Entity
StackTrace:
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)

The clue is the contents of the defaultContainerName parameter: you will see that in the App.config of your Entity Framework assembly.

Copy that over to the App.config of your main assembly, then make sure it points to your right database (if you use OTAP), then go :-)

Your App.config then looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MyEntities" connectionString="metadata=res://*/My_EntityModel.csdl|res://*/My_EntityModel.ssdl|res://*/My_EntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=my-sql-server;initial catalog=my-database;persist security info=True;user id=my-user;password=my-password;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: