简体   繁体   English

.NET 4:如何在Web.Config中的其他程序集中配置EDMX文件

[英].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. 我在配置EDMX文件时遇到问题,该文件位于其他程序集中,而不是Web项目中。 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. 在Visual Studio中,Project 1中的.EDMX文件的更新顺利进行,而当我在项目2中有.EDMX文件时,应用程序按预期运行。

Anyone has an idea on how to configure the .EDMX file inside Project 1 to point to the connectionstring of Web.Config? 任何人都知道如何配置Project 1中的.EDMX文件指向Web.Config的连接字符串? (or should I use Project1.dll.config to configure Project 1?) (或者我应该使用Project1.dll.config配置Project 1?)

You have to change the * in the connection string for the assembly name where the .edmx files are in: 您必须更改.edmx文件所在的程序集名称的连接字符串中的*

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

As it turned out, there were 2 problems. 事实证明,有2个问题。 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/ 第二个问题是这里描述的问题: 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 它与路径.csdl,.ssdl和.msl文件在Project1程序集中作为资源有关

Anyway, things function properly now 无论如何,事情现在正常运作

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. 更简单的方法是从Web.Config获取连接字符串并将它们复制到App.Config中,并将EDMX的连接字符串指向相同的DB信息。 eg 例如

  <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. 如果您已将Database.edmx从Project 2移动到Project 1,则还需要检查名称空间是否正确,您可以通过打开Database.edmx和goto代码来检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM