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

谁说我不能喝 提交于 2019-11-28 00:22:47

问题


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?)


回答1:


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" />



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/14213943/net-4-how-to-configure-edmx-file-in-other-assembly-in-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!