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="data source=my-sql-server;initial catalog=my-database;persist security info=True;user id=my-user;password=my-password;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings> </configuration>
–jeroen
Leave a Reply