简体   繁体   English

我可以在同一Visual Studio解决方案中使用.NET 4和.NET 4.5吗(使用Entity Framework时)?

[英]Can I target .NET 4 and .NET 4.5 in the same Visual Studio solution (when using Entity Framework)?

I have a Visual Studio 2012 solution that consists of both a web application and some client-side code. 我有一个Visual Studio 2012解决方案,其中包含一个Web应用程序和一些客户端代码。 The way the application is structured, there is a shared class library used for data access. 应用程序的结构方式是,有一个共享的类库用于数据访问。

The client-side code needs to target .NET 4, since I'll be deploying that to machines that may not have .NET 4.5. 客户端代码需要以.NET 4为目标,因为我会将其部署到可能没有.NET 4.5的计算机上。 Since I control the web server end, I can assume .NET 4.5 for that. 由于我控制着Web服务器端,因此我可以假设使用.NET 4.5。

Now, the data-access library needs to be used by both the client and server end, and so it needs to use only .NET 4. However, this is causing me unexpected problems. 现在,客户端和服务器端都需要使用数据访问库,因此它只需要使用.NET4。但是,这导致了我意想不到的问题。

The data-access library uses Entity Framework. 数据访问库使用实体框架。 Being .NET 4, it uses EF version 4.4. 作为.NET 4,它使用EF版本4.4。 However, the web application also needs to reference the Entity Framework, and being .NET 4.5 it uses EF version 5.0. 但是,该Web应用程序还需要引用Entity Framework,在.NET 4.5中,它使用EF版本5.0。

This results in an (intermittent) error when I try to run the application. 当我尝试运行应用程序时,这会导致(间歇性)错误。 (I'm starting both the website and the client code from VS2012, on the same machine). (我正在同一台计算机上同时从VS2012启动网站和客户端代码)。

The website works OK if I just enter a URL into the address bar on the browser - its able to use the data-access library to fetch data without problems. 如果我仅在浏览器的地址栏中输入一个URL,该网站就可以正常运行-它能够使用数据访问库来毫无问题地获取数据。 However, when I use the client application, it (usually, not always!) gets a server error back from the web end saying that it (the web end) can't load the Entity Framework assembly: 但是,当我使用客户端应用程序时,它(通常,并非总是如此!)从Web端返回服务器错误,说它(Web端)无法加载Entity Framework程序集:

Could not load file or assembly 'EntityFramework, Version=5.0.0.0, ... The located assembly's manifest definition does not match the assembly reference. 无法加载文件或程序集'EntityFramework,Version = 5.0.0.0,... ...找到的程序集的清单定义与程序集引用不匹配。

I'm not sure whether this is only a problem because I'm running in the debugger, on the same machine, or whether this is something that's going to affect my application once deployed. 我不确定这是否仅仅是一个问题,因为我是在同一台机器上的调试器中运行,还是一旦部署就会​​影响我的应用程序。

Am I doing something crazy here, or should this work OK? 我在这里做疯狂的事情,还是应该正常工作?

For anyone attempting this: it does work so long as you do a "build solution" prior to hitting "run" (which in my case starts the client-side application in debug mode, but starts the web app without debugging). 对于任何尝试此操作的人:只要您在单击“运行”之前执行“构建解决方案”,它便会起作用(在我的情况下,这将以调试模式启动客户端应用程序,但无需调试即可启动Web应用程序)。 To debug the web app too, I need to explicitly start that (right-click, debug), and then start the client-side app (right-click, debug). 要同时调试Web应用程序,我需要显式启动该应用程序(单击鼠标右键,调试),然后启动客户端应用程序(单击鼠标右键,调试)。 Again, a "build solution" is required every time (and occasionally a "rebuild"). 同样,每次都需要“构建解决方案”(有时需要“重建”)。

EDIT : aaaannnndd... suddenly it stopped working. 编辑 :aaaannnndd ...突然停止工作。 However, I figured out that when I run the client-side application within VS, it builds/updates the EF DLL in the web project bin directory with the one used by the client app, which causes the version-mismatch as soon as the website is accessed, because the website is expecting the later version. 但是,我发现,当我在VS中运行客户端应用程序时,它会使用客户端应用程序所使用的目录在Web项目bin目录中构建/更新EF DLL,这会在网站出现后立即导致版本不匹配被访问,因为该网站需要更高版本。 To resolve this, I now have a batch file that copies in the correct version of the EF DLL into the website bin directory. 要解决此问题,我现在有一个批处理文件,它将正确版本的EF DLL复制到网站bin目录中。 So, I can start the client application, then execute my batch file, then start the website. 因此,我可以启动客户端应用程序,然后执行我的批处理文件,然后启动网站。 A pain, but at least it now works reliably. 很痛苦,但至少现在可以正常工作了。

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

相关问题 无法在Visual Studio 2013中定位.NET Framework 4.5 - Unable to target .NET Framework 4.5 in Visual Studio 2013 我可以使用Visual Studio 2015来定位.net框架4.7吗? - Can I use Visual Studio 2015 to target .net framework 4.7? 在Visual Studio 2010解决方案级别定位.NET框架的特定版本 - Target specific version of .NET framework at Visual Studio 2010 Solution level 是否可以在Visual Studio 2012中使用.NET 4.5框架构建Outlook 2010加载项? - Can you build an Outlook 2010 add in using .NET 4.5 framework in Visual Studio 2012? Visual Studio 无法定位 .NET 框架 4.8 - Visual Studio Can't Target .NET Framework 4.8 .NET 4.5中的Visual Studio解决方案中的项目引用 - Project References within a Visual Studio Solution in .NET 4.5 安装Visual Studio 2010,我只能选择.NET 4作为我的目标框架 - Installed Visual Studio 2010 and I can only choose .NET 4 as my target framework .net 5 在 Visual Studio 16.8 中不能用作目标框架 - .net 5 not available as Target Framework in Visual Studio 16.8 在Visual Studio 2017中选择.NET Target Framework - Selecting .NET Target Framework in Visual Studio 2017 Visual Studio 2012 InstallShield LE .NET Framework 4.5先决条件 - Visual Studio 2012 InstallShield LE .NET Framework 4.5 Prerequisites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM