Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an Entity Framework Connection require a metadata property?

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!

like image 482
John B Avatar asked Jan 30 '09 19:01

John B


People also ask

What is metadata in connection string?

The Metadata parameter contains a list of locations for the EntityClient provider to search for model and mapping files. Model and mapping files are often deployed in the same directory as the application executable file.

Is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists?

The entity type 'ProductStockRequirements' is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists. If all nullable properties contain a null value in database then an object instance won't be created in the query.

Does Entity Framework keep connection open?

Entity Framework will handle database connections automatically by default. Note two things here: EF will open the connection if you specify any LINQ or ObjectQuery method, and that connection won't be closed until the ObjectResult has been completely consumed or disposed.


2 Answers

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.

like image 155
Dave Swersky Avatar answered Oct 16 '22 21:10

Dave Swersky


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.

like image 29
Randolpho Avatar answered Oct 16 '22 20:10

Randolpho