Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4: How to configure EDMX file in other assembly in Web.Config

I have a problem with configuring an EDMX file that lives in an other assembly than by web project. My project looks somewhat like this:

Project 1
--> Database.edmx
--> App.Config

Project 2
--> Ton's of .cs and .aspx files.
--> Web.Config with the proper connection string.

Inside Visual Studio the updating of the .EDMX file inside Project 1 goes smoothly and while I had the .EDMX file inside project 2 the application ran as it supposed to.

Anyone has an idea on how to configure the .EDMX file inside Project 1 to point to the connectionstring of Web.Config? (or should I use Project1.dll.config to configure Project 1?)

like image 606
Erwin Avatar asked Jan 08 '13 11:01

Erwin


3 Answers

You have to change the * in the connection string for the assembly name where the .edmx files are in:

<add name="Entities" connectionString="metadata=res://*/Models.EF.Model.csdl|res://*/Models.EF.Model.ssdl|res://*/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />

for

<add name="Entities" connectionString="metadata=res://Project2/Models.EF.Model.csdl|res://Project2/Models.EF.Model.ssdl|res://Project2/Models.EF.Model.msl;provider=System.Data.SqlClient;provider connection ... ;" providerName="System.Data.EntityClient" />
like image 143
Carles Company Avatar answered Nov 02 '22 09:11

Carles Company


As it turned out, there were 2 problems. One was solved replacing the * in the connection string.

The second problem was the one described here: http://blogs.teamb.com/craigstuntz/2010/08/13/38628/

It had to do with the path .csdl, .ssdl and .msl files had as resources inside the Project1 assembly

Anyway, things function properly now

like image 23
Erwin Avatar answered Nov 02 '22 07:11

Erwin


Easier way is to take connection string from Web.Config and copy them into App.Config and point EDMX's connectionstring to same DB information. e.g

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
    <add name="aspnetdbEntities" connectionString="metadata=res://*/Data.PSBData.csdl|res://*/Data.PSBData.ssdl|res://*/Data.PSBData.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\aspnetdb.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

Also you need to check if the namespaces are correct if you have moved Database.edmx from Project 2 to Project 1, which you can check by opening Database.edmx and goto code behind.

like image 1
Nexus23 Avatar answered Nov 02 '22 07:11

Nexus23