views:

4601

answers:

3

I switched my DAL from using LINQ over to Entity Framework. Because my application connects to different databases depending on the current user, I need to dynamically create the DataContext at run time and pass in the appropriate connection string. However, when I tried to programatically create an Entity Framework connection using my old connection string, the connection failed. It complained that it didn't recognize the key in the connection string, "server" to be exact.

I found out that I needed to do this in order to get the Entity Framework connection to work:

EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/xxxxxxxxxx.csdl...";
Entities entities = new Entities(entityBuilder.ToString());

Why is this?
What is the Metadata property for?
Is it going to be a problem that its always the same for multiple different connections?
What should it be?
Is there any way around this?

Thanks in advance!

Update 1: Thanks for the update Randolpho, but...
The whole reason I'm having this issue, is that I can't store the connection strings in a configuration file. The connection string is dynamically determined at runtime by which user is connecting.

Here is my exact scenario:
If user A is connecting, the app pulls data from database A. If user B is connecting, the app pulls data from database B.
The connection strings are stored in a main database, and the number is potentially limitless. Every time I add a user, I don't want to have to go into the web.config, not to mention the fact that it would eventually get HUGE!

+2  A: 

You will find these links very informative:

http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx

http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata.aspx

Bottom line? Entity Framework needs the metadata to build your entity mappings.

Additionally, you should consider moving your connection information out to your configuration file rather than build it in code. The first link will show you how to do that.

Randolpho
+1 - One more reason I won't be using it.
Otávio Décio
@ocdecio: Don't discount EF just for that; NHibernate works pretty much the same way. I'm sure there are other reasons, though. :)
Randolpho
@ocdecio, I'd love to see an ORM mapper that doesn't use some sort of mapping file. Unless your doing 1:1 with tables something has to tell something what todo.
jfar
+2  A: 

Expanding on Randolpho's answer:

The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.

Dave Swersky
A: 

Since the mappings are the same for each connection, and it wont change, it shouldn't matter that I use the same Metadata for all of them right?

SkippyFire