Friday, November 16, 2007

Making Entity Framework work with a Web Application

Making Entity Framework (EFW) work with a web application was the toughest (relatively) of the lot (Win App, Console App etc).

To get it working for any type of app, these 2 steps are sufficient:
  1. Add the app.config of the EFW project to your app. If you already have a app.config, then add only the "connectionStrings" node of it.
  2. Copy the c/m/s (.csdl, .msl and .ssdl) files to the bin directory, where the executable of the app resides.

But if you have to get this working for a web app, then

  1. Add the "connectionStrings" node to your web app's web.config.
  2. Copy c/m/s files to App_Data folder
  3. Change the string in "connectionStrings" node From
    ".\EFWSampleDb.csdl.\EFWSampleDb.ssdl.\EFWSampleDb.msl"
    To
    "DataDirectoryEFWSampleDb.csdlDataDirectoryEFWSampleDb.ssdlDataDirectoryEFWSampleDb.msl"

Alternately, instead of "|DataDirectory|EFWSampleDb.csdl" you can also say "~\App_Data\EFWSampleDb.csdl". ("~" refers to the root directory of the web app). So, this essentially means that you can have the m/s/l files in any directory, and not necessarily in App_Data, and reference it in web.config.


Failing to do the above steps may error out saying:

"The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource."


OR

"The specified named connection is either not found in the configuration, not intended to be used with the EntityClient Provider, not valid."

4 comments:

Grumpy Grandma said...

When I do that, the metadata error msg goes away, from I see this error:

Unknown model type MyCompany.Model.CompanyDb

for ADO.NET Entities Beta 3.
Did you get these two working together?

Glen McGregor said...

Where did you get the c/s/m files from? I only get an edmx file

Anonymous said...

Glen - look at the output build directory...

alex said...

Thank you very much for this information, this was really helpful, had hours trying to find a solution.
Thanks
Alex :D