简体   繁体   English

跨多个项目共享实体框架库

[英]Sharing Entity Framework Library across multiple projects

We have an Entity Framework project with several models set up using .NET 4 and VS2010. 我们有一个Entity Framework项目,其中包含使用.NET 4和VS2010设置的几个模型。 Then we have several projects that need to use this entity project. 然后我们有几个项目需要使用这个实体项目。 We successfully compiled the EF project into a DLL. 我们成功地将EF项目编译为DLL。 We have also successfully added the EF dll reference into multiple projects which is working great. 我们还成功地将EF dll参考添加到多个项目中,这些项目运行良好。

The problem is now that we have several programs (ASP.NET and console apps) that reference this EF dll and the dll is copied locally for each calling program, when we make a change to the EF dll, we then have to go into each and every project and replace the EF dll with the new build. 问题是现在我们有几个程序(ASP.NET和控制台应用程序)引用这个EF dll并且dll在本地为每个调用程序复制,当我们对EF dll进行更改时,我们必须进入每个和每个项目,并用新版本替换EF dll。

I've done a lot of searching for sharing libraries and even EF projects across multiple projects. 我已经做了很多搜索在多个项目中共享库甚至EF项目。 While I have found several, I can't seem to find a good example that I can make work for my situation or that isn't so old that it's irrelevant. 虽然我找到了几个,但我似乎无法找到一个很好的例子,我可以为我的情况工作,或者不是那么老,以至于无关紧要。

That is the general issue I'm having. 这是我遇到的一般问题。 To give a better idea of the issues I'm encountering, I will focus on one particular project. 为了更好地了解我遇到的问题,我将重点关注一个特定的项目。 This is an ASP.NET webform project for in intranet. 这是Intranet中的ASP.NET webform项目。 If we add the EF dll reference and allow the project to copy the dll locally, the EF works fantastically. 如果我们添加EF dll引用并允许项目在本地复制dll,则EF可以很好地工作。 However, because we have multiple project we now need to try and centralize the EF dll somewhere where it can be shared by the multiple processes. 但是,因为我们有多个项目,所以我们现在需要尝试将EF dll集中在可由多个进程共享的地方。 I am not trying to set this up so that one EF dll is accessed across multiple server. 我不是试图设置它,以便跨多个服务器访问一个EF dll。 I am happy to install a copy of the DLL on individual servers if necessary. 如有必要,我很乐意在各个服务器上安装DLL的副本。

My desire is to create a "common libraries" directory on each server, simplified example "C:\\OurLibraries". 我的愿望是在每个服务器上创建一个“公共库”目录,简化示例“C:\\ OurLibraries”。 We would then put or EF dlls (and maybe others later) into this folder and allow the various programs/processes to access the common copy of the EF dll. 然后我们将或者EF dll(以及后来的其他人)放到这个文件夹中,并允许各种程序/进程访问EF dll的公共副本。 I've made sure the "local copy" of the EF dll has been removed from the intranet project and added a reference to the "C:\\OurLibraries\\OurEF.dll" file. 我已确保已从Intranet项目中删除了EF dll的“本地副本”,并添加了对“C:\\ OurLibraries \\ OurEF.dll”文件的引用。 Everything builds fine and the intranet project works fine until it tries to display a page that has references the EF and then displays an error message: 一切都很好,Intranet项目工作正常,直到它尝试显示一个引用EF的页面,然后显示一条错误消息:

"Could not load type 'EntityNS.ProductDBEntity'." “无法加载类型'EntityNS.ProductDBEntity'。”

If I turn on "local copy" in the reference, the intranet site works fine again. 如果我在引用中打开“本地副本”,则Intranet站点将再次正常工作。 I cannot seem to find that magic setting that will allow me to share the EF dll. 我似乎无法找到允许我分享EF dll的魔法设置。

I have tried the following things based on various posts, but with no success: 我根据各种帖子尝试了以下内容,但没有成功:

  • Signing the assembly and adding to the GAC. 签署程序集并添加到GAC。 Experienced the same issue as having it in "C:\\OurLibraries" 遇到过与“C:\\ OurLibraries”相同的问题

  • Adding the "C:\\OurLibraries" directory to the PATH environment variable. 将“C:\\ OurLibraries”目录添加到PATH环境变量中。

  • Changed my connection string for the EF in my intranet web.config file to remove the "OR's" from the string: /Ecomedate.csdl|res:///Ecomedate.ssdl|res:///Ecomedate.msl;provider=System... to ;provider=System... 在我的intranet web.config文件中更改了EF的连接字符串,以从字符串中删除“OR”:/Ecomedate.csdl |res:///Ecomedate.ssdl|res:///Ecomedate.msl;provider=System ...到;提供者=系统......

(based on this post: Sharing Entity framework objects across projects? ) (基于这篇文章: 跨项目共享实体框架对象?

I've spent many hours working on this and searching forums and posts. 我花了很多时间研究这个并搜索论坛和帖子。 I know there has got to be a way to do this otherwise code reuse and DLL sharing seems useless, so any help you can suggest would be appreciated. 我知道必须有一种方法来做这个,否则代码重用和DLL共享似乎没用,所以任何你可以建议的帮助将不胜感激。

Here is are additional efforts that I have made and in response to some of the post so far. 以下是我所做的额外努力以及对迄今为止的一些帖子的回应。

Also here is what I have experienced with the GAC so far. 这也是我到目前为止在GAC中所经历的。 - on a computer with VS2010 installed, the gacutil is located in C:\\Program Files\\Microsoft SDK... and from forum on questions "Where is the gacutil" the general tone is that gacutil is now considered a dev tool and not intended for use in prod environments. - 在安装了VS2010的计算机上,gacutil位于C:\\ Program Files \\ Microsoft SDK ...以及论坛上的问题“gacutil在哪里”的一般基调是gacutil现在被认为是开发工具而不是意图用于生产环境。 Gacutil is not part of the Server 2008 or .Net 4 framework, so there are several suggestions on how to deploy and deal with GAC dlls 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. 首先,旧的安装方式,使用gacutil,但通过使用psexec复制并调用生产服务器上的gacutil。 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. 我可以让psexec从本地开发盒运行gacutil到prod服务器并获得返回代码0,成功,但是我找不到实际查看它安装在生产服务器上的方法,因为没有gacutil在prod服务器上,我不能使用像gacutil / l DataEntity.dll这样的东西查看已安装dll的信息...如果它甚至安装正确。
  • I tried copying the gacutil.exe and gacutil.exe.config files to production server to try and run from there. 我尝试将gacutil.exe和gacutil.exe.config文件复制到生产服务器以尝试从那里运行。 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. 当程序运行并提供gacutil的版本号时,它不响应任何命令行开关,如gacutil.exe / i DataEntity.dll或gacutil.exe / l DataEntity。 It just displays the gacutil version info again and stops. 它只是再次显示gacutil版本信息并停止。
  • Someone suggesed on a forum to installing the Microsoft SDK on the prod machine. 有人在论坛上建议在prod机器上安装Microsoft SDK。 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. 虽然我可能不得不考虑到目前为止由于缺乏成功,但我真的不喜欢在我的生产环境中安装SDK的想法。
  • 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. 我试图查找远程GAC管理器等工具来查看和管理,但该开源项目的最后一次开发是2008年,所以当我尝试使用它来查看GAC时,它希望向我展示c:\\ Windows \\程序集gac dll,但.NET 4现在使用C:\\ windows \\ Microsoft.NET \\ assembly存储GAC dll,所以我似乎可以找到任何方法来查看或维护远程生产服务器的gac上的DLL。 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. 如果我在命令提示符下的c:\\ windows下运行dir DataEntity.dll / s命令,我会发现嵌入在C:\\ Windows \\ Microsoft.NET \\ assembly \\ GAC_MSIL目录中的dll,但如果我试着查看通过C:\\ Windows \\ Microsoft.NET \\ assembly \\ GAC_MSIL中的资源管理器文件,我看不到dll,所以我找不到允许我在C:\\ Windows \\ Microsoft中管理(安装,列出,卸载)DLL的工具服务器2008生产服务器上的.NET \\ assembly \\ GAC_MSIL。
  • There was a suggestion to install dlls into gac via drag and drop. 有人建议通过拖放将dll安装到gac中。 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? 复制到C:\\ Windows \\ Microsoft.NET \\ assembly \\ GAC_MSIL目录也可以吗? 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. 我已经尝试过了,但是因为我找不到一个能让我看到已安装/注册的DLL的工具,我无法判断它是否有效。
  • another suggestion was to create an installer that would just install into the GAC. 另一个建议是创建一个只安装到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; 我无法弄清楚如何从GAC卸载旧的DLL然后在gac中安装新版本的DLL; 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. 其次,当我尝试卸载dll时,它一直说它正在被另一个应用程序使用。 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. 我终于想通了它是IIS,不得不关闭IIS,卸载,重启,安装,然后重启IIS。 This is a pain in the but to try and automate. 这是一个痛苦,但尝试和自动化。

Seems like there should be a better way to deploy dlls to a production environment into a shared directory. 似乎应该有一种更好的方法将dll部署到生产环境到共享目录中。 I simply want to try and put the DataEntity.dll in ac:\\MyLibraries directory and have the processes access that one copy of the DLL. 我只是想尝试将DataEntity.dll放在ac:\\ MyLibraries目录中,让进程访问DLL的一个副本。 Microsoft does it with the C:\\Program Files\\Common Files, so it should be possible, but I have now spent days trying to find a way that works that would considerabley reduce the maintenance efforts imposed by the GAC or installer options, reduce the number of duplicate dlls, and avoiding overlooking replacing dlls if allowed to 'copy locally'. 微软用C:\\ Program Files \\ Common Files做到了,所以它应该是可能的,但我现在花了几天时间试图找到一种可以大大减少GAC或安装程序选项所带来的维护工作的方法,减少重复dll的数量,并且如果允许“在本地复制”,则避免忽略替换dll。

the best solution for your problem is using Web Services .web services are created for this purpose.You can build a WCF service library and then use it's methods in all of your projects. 您的问题的最佳解决方案是使用Web Services 。为此目的创建.web服务。您可以构建一个WCF服务库,然后在所有项目中使用它的方法。

Good Luck 祝好运

这可能看起来很“外面”作为解决方案,但我们正在考虑使用Git存储库对多个服务器进行远程发布,Git存储库将使用最新的DLL(并且只有DLL)提交,然后推送到每个生产服务器/应用服务器。

The GAC approach is probably the closest one to what you are looking for. GAC方法可能是您所寻找的最接近的方法。 Since you were unable to get the GAC working, you should double check to makes sure you followed the instructions for installing in the GAC. 由于您无法使GAC正常工作,因此您应仔细检查以确保遵循GAC中的安装说明。

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

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