Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Entity Framework Reference from MVC project

Tags:

Apologies if this has already been answered, but after 2 hours of research I am yet to find a definitive answer to this issue. Hopefully asking will yield results.

I'm "somewhat better than beginner" with MVC and EF, but I do understand overall application architecture. You see, for even greater Separation of Concerns and future planned changes, I'd like to decouple my MVC project from Entity Framework. Here's what I have so far:

  • Models project (full of well-annotated POCOs, as containers, no business logic)
  • DataAccess project (references my Models project, and Entity Framework)
  • MVC Web project (which references my Models project only)

I've already created my Repository pattern (in the DataAccess project), and tested it using a separate UnitTest project. 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). 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:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

How can I design this so I can completely remove the Entity Framework reference from the Repository consumer (UnitTest or MVC Web projects)? I want to do everything via my Repository, without the MVC project knowing anything about my ORM technology.

like image 980
Chinedu Opara Avatar asked Aug 22 '16 13:08

Chinedu Opara


1 Answers

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. The only way to avoid a reference is to 100% segregate the code. 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. If your DAL depends on EF and your MVC project depends on your DAL, then you have an inherent dependency on EF anyways.

like image 75
Chris Pratt Avatar answered Oct 10 '22 21:10

Chris Pratt