简体   繁体   English

从MVC项目中删除实体框架参考

[英]Removing Entity Framework Reference from MVC project

Apologies if this has already been answered, but after 2 hours of research I am yet to find a definitive answer to this issue. 如果已经回答道歉,但经过2个小时的研究后,我还没有找到这个问题的明确答案。 Hopefully asking will yield results. 希望询问会产生结果。

I'm "somewhat better than beginner" with MVC and EF, but I do understand overall application architecture. 我使用MVC和EF“比初学者好”,但我确实了解整体应用程序架构。 You see, for even greater Separation of Concerns and future planned changes, I'd like to decouple my MVC project from Entity Framework. 您可以看到,对于更大的关注点和未来计划的更改,我想我的MVC项目与实体框架分离。 Here's what I have so far: 这是我到目前为止所拥有的:

  • Models project (full of well-annotated POCOs, as containers, no business logic) 模型项目(充满注释良好的POCO,作为容器,没有业务逻辑)
  • DataAccess project (references my Models project, and Entity Framework) DataAccess项目(引用我的模型项目和实体框架)
  • MVC Web project (which references my Models project only) MVC Web项目(仅引用我的Models项目)

I've already created my Repository pattern (in the DataAccess project), and tested it using a separate UnitTest project. 我已经创建了我的Repository模式(在DataAccess项目中),并使用单独的UnitTest项目对其进行了测试。 It all works. 一切正常。

Problem is, the consumer (the UnitTest project, in this case) still needs a nuget reference to Entity Framework (same with the MVC Web project). 问题是,消费者(在这种情况下为UnitTest项目)仍然需要对Entity Framework的nuget 引用 (与MVC Web项目相同)。 When I remove the EF reference from my Test project (and instead hard-code my connection string when passing it into my DataAccess project), I get this error: 当我从我的Test项目中删除EF引用时(而是在将其传递到我的DataAccess项目时硬编码我的连接字符串)时,我收到此错误:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. 没有为ADO.NET提供程序找到具有不变名称“System.Data.SqlClient”的实体框架提供程序。 Make sure the provider is registered in the 'entityFramework' section of the application config file. 确保提供程序已在应用程序配置文件的“entityFramework”部分中注册。

How can I design this so I can completely remove the Entity Framework reference from the Repository consumer (UnitTest or MVC Web projects)? 我如何设计这个,以便我可以从Repository使用者(UnitTest或MVC Web项目)中完全删除Entity Framework引用 I want to do everything via my Repository, without the MVC project knowing anything about my ORM technology. 我希望通过我的存储库完成所有工作,而MVC项目却不了解我的ORM技术。

This is a bit misguided. 这有点误导。 First, the reason a reference to Entity Framework is required is because you're leaking code from that library into your MVC project. 首先,需要引用实体框架的原因是因为您将该库中的代码泄漏到MVC项目中。 The only way to avoid a reference is to 100% segregate the code. 避免引用的唯一方法是100%隔离代码。 Which is potentially possible, but not easy by any stretch. 这可能是可能的,但任何时候都不容易。 However, that's not important anyways. 但是,这并不重要。 Having a reference to Entity Framework is meaningless. 引用实体框架是没有意义的。 The important part of N-tier design is segregating distinct functionality , not necessarily references. N层设计的重要部分是隔离不同的功能 ,不一定是参考。 If your DAL depends on EF and your MVC project depends on your DAL, then you have an inherent dependency on EF anyways. 如果您的DAL依赖于EF并且您的MVC项目取决于您的DAL,那么您对EF有固有的依赖性。

The problem is that Visual Studio strips all references that are not used in the project. 问题是Visual Studio会删除项目中未使用的所有引用。 This solved the problem for me: 这解决了我的问题:

public DbContext() 
    :base()
{
   var a = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
}

Even after removing NuGet package you might be having residual props in your configs (packages and/or web) related with EF. 即使在删除NuGet包之后,您可能在与EF相关的配置(包和/或Web)中有剩余道具。 It's also worth checking your csproj file as well. 它也值得检查你的csproj文件。 The issue that you are having is the lack of a line like this in your config. 您遇到的问题是您的配置中缺少这样的行。

<entityFramework>
    ...    
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

That being said, this still shows that your project is actually bound to EF afaik. 话虽这么说,这仍然表明你的项目实际上是绑定到EF afaik。

You could try accessing your services via a Web API, which acts as a buffer to the Data Access Layer(s) and deals in View Models passed back and forth as anonymous JSON objects. 您可以尝试通过Web API访问您的服务,Web API充当数据访问层的缓冲区,并作为匿名JSON对象来回传递的View Models中的交易。 Your front end (website) can then be totally isolated from the details behind the API and just act as a client to it. 然后,您的前端(网站)可以完全与API背后的细节隔离,并充当它的客户端。 Angular JS / Angular (2) lend themselves well to this approach as I'm sure many other client side technologies do. Angular JS / Angular(2)非常适合这种方法,因为我确信许多其他客户端技术都可以。

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

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