c# - 跨多个项目共享 Entity Framework 库

标签 c# asp.net entity-framework-4

我们有一个 Entity Framework 项目,其中包含使用 .NET 4 和 VS2010 设置的多个模型。然后我们有几个项目需要用到这个实体项目。我们成功地将 EF 项目编译成 DLL。我们还成功地将 EF dll 引用添加到了运行良好的多个项目中。

现在的问题是我们有几个程序(ASP.NET 和控制台应用程序)引用了这个 EF dll,并且每个调用程序都在本地复制了 dll,当我们对 EF dll 进行更改时,我们必须进入每个和每个项目,并用新版本替换 EF dll。

我已经在多个项目中搜索了很多共享库甚至 EF 项目。虽然我找到了几个,但我似乎找不到一个很好的例子,我可以为我的情况做工作,或者它不是太旧以至于无关紧要。

这是我遇到的普遍问题。为了更好地了解我遇到的问题,我将专注于一个特定的项目。这是一个用于 Intranet 的 ASP.NET webform 项目。如果我们添加 EF dll 引用并允许项目在本地复制 dll,则 EF 工作得非常好。但是,因为我们有多个项目,我们现在需要尝试将 EF dll 集中在可以由多个进程共享的某个地方。我不是要设置它以便跨多个服务器访问一个 EF dll。如有必要,我很乐意在各个服务器上安装 DLL 的副本。

我的愿望是在每个服务器上创建一个“公共(public)库”目录,简化示例“C:\OurLibraries”。然后,我们会将 EF dll(也许以后还有其他)放入该文件夹中,并允许各种程序/进程访问 EF dll 的公共(public)副本。我已确保 EF dll 的“本地副本”已从 Intranet 项目中删除,并添加了对“C:\OurLibraries\OurEF.dll”文件的引用。一切正常,并且 Intranet 项目运行良好,直到它尝试显示引用 EF 的页面,然后显示错误消息:

“无法加载类型 'EntityNS.ProductDBEntity'。”

如果我在引用中打开“本地副本”,则 Intranet 站点再次正常工作。我似乎找不到允许我共享 EF dll 的神奇设置。

我根据各种帖子尝试了以下内容,但没有成功:

  • 签署程序集并添加到 GAC。遇到与在“C:\OurLibraries”中遇到相同的问题
  • 将“C:\OurLibraries”目录添加到 PATH 环境变量中。
  • 在 Intranet web.config 文件中更改了 EF 的连接字符串,以从字符串中删除“OR”:
    /Ecomedate.csdl|res:///Ecomedate.ssdl|res:///Ecomedate.msl;provider=System...

    ;provider=系统...

  • (基于这篇文章:Sharing Entity framework objects across projects?)

    我花了很多时间在这方面工作并搜索论坛和帖子。我知道必须有一种方法可以做到这一点,否则代码重用和 DLL 共享似乎毫无用处,因此我们将不胜感激。

    以下是我所做的额外努力,并回应了迄今为止的一些帖子。

    这也是我迄今为止在 GAC 中的经历。
    - 在安装了 VS2010 的计算机上,gacutil 位于 C:\Program Files\Microsoft SDK... 并且从关于“gacutil 在哪里”问题的论坛中,总体基调是 gacutil 现在被认为是一个开发工具而不是预期的用于生产环境。 Gacutil 不是 Server 2008 或 .Net 4 框架的一部分,因此有几个关于如何部署和处理 GAC dll 的建议

    • first, the old way of installing, using the gacutil, but by using psexec to copy and call gacutil on the production server. I can get psexec to run the gacutil from a local dev box to a prod server and get a return code of 0, success, however I cannot find a way to actually view that it is installed on the production server, because there is not gacutil on prod server, I can't use someting like gacutil /l DataEntity.dll to view info on installed dll...if it even installed correctly.
    • I tried copying the gacutil.exe and gacutil.exe.config files to production server to try and run from there. While the program run and gives the version number of the gacutil, it doesn't respond to any command line switches such as gacutil.exe /i DataEntity.dll or gacutil.exe /l DataEntity. It just displays the gacutil version info again and stops.
    • Someone suggesed on a forum to installing the Microsoft SDK on the prod machine. While I might have to consider this due to lack of success so far, I really don't like the idea of installing an SDK on my production evironments.
    • I tried to find tools such as the Remote GAC Manager to view and manage, but the last development on that opensource project was 2008, so when I try to use it to veiw the GAC, it is wanting to show me c:\Windows\assembly gac dlls, but .NET 4 now uses C:\windows\Microsoft.NET\assembly to store GAC dlls, so I can's seem to find any way to view or maintain DLLs on the remote production server's gac. If I run a dir DataEntity.dll /s command at c:\windows in the command prompt, I find the dll embedded in the C:\Windows\Microsoft.NET\assembly\GAC_MSIL directory, but if I try to look at the file through explorer in C:\Windows\Microsoft.NET\assembly\GAC_MSIL, I cannot see the dll, so I cannot find a tool that allows me to manage (install, list, uninstall) the DLLs in C:\Windows\Microsoft.NET\assembly\GAC_MSIL on the server 2008 production server.
    • There was a suggestion to install dlls into gac via drag and drop. I am trying to automate our deployment process, so having to manually drag and drop doesn't make a lot of sense. Does a copy to the C:\Windows\Microsoft.NET\assembly\GAC_MSIL directory work too? I've tried it, but again since I cannot find a tool that will let me see the installed/registered DLLs I can't tell if it worked or not.
    • another suggestion was to create an installer that would just install into the GAC. I tried this method and ran into a couple of issues. First it was a very manual process. I could not figure out how to get it to uninstall the old dll from GAC and then install the new version of DLL in gac; it kept insisting that I uninstall the previous installation first. Second, when I tried to uninstall the dll, it kept saying that it was in use by another application. I tried restarting and then uninstalling it, but no go. I finally figured out it was IIS and had to shutdown IIS, uninstall, restart, install, and then restart IIS. This is a pain in the but to try and automate.


    似乎应该有更好的方法将 dll 部署到生产环境到共享目录中。我只是想尝试将 DataEntity.dll 放在 c:\MyLibraries 目录中,并让进程访问该 DLL 的副本。 Microsoft 使用 C:\Program Files\Common Files 来做到这一点,所以它应该是可能的,但我现在花了几天时间试图找到一种有效的方法,可以大大减少 GAC 或安装程序选项强加的维护工作,减少重复 dll 的数量,如果允许“本地复制”,则避免忽略替换 dll。

    最佳答案

    您问题的最佳解决方案是使用 Web Services .web 服务就是为此目的而创建的。您可以构建一个 WCF 服务库,然后在您的所有项目中使用它的方法。

    祝你好运

    关于c# - 跨多个项目共享 Entity Framework 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9552203/

    相关文章:

    c# - 获取使用javascript计算的按钮单击事件上的gridview模板字段标签值

    c# - 如何设置隐藏代码中中继器内 anchor 标记的 href 属性?

    c# - 为什么我的 RangeValidator 不工作?

    WPF 绑定(bind)到 Entity Framework 不更新

    c# - Entity Framework 4 : convert a string condition to a lambda expression?

    c# - 如何在 WinForms 中等待信号同时监听事件?

    c# - 使用 Entity Framework 在运行时动态选择列

    c# - 单步执行测试时测试成功;否则不

    c# - 将值从窗口返回到 WPF 类

    c# - EF 4.0 中看似无限的堆栈跟踪和负载下的查询性能不佳