简体   繁体   中英

3-layer architecture. EF in presentation layer

I try to create 3-layer architecture application. There are 2 class library (data access, business logic)and asp.net MVC (presentation). For DB connection I use entity framework. In DataAccess I configured DatabaseContext my connection string store in web.config. If I try add raw in database from Presentation I get error

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'."

How I understand it is bad practice install EF in presentation level.I think about add reference System.Data.SqlClient but think it's bad practice too. Maybe someone met a similar problem. I'll be greateful for any help.

If your sticking to a true three tier architecture and using interfaces and dependency injection between the layers then the problem is most likely that visual studio doesn't see that you're actually using the data layer and doesn't deploy the dependencies. I've studied this issue a few years back and it's a bit painful to resolve. I'm not absolutely sure if this is your problem due to the minimal information you provided but if it sounds like your problem, you can test by adding a hard dependency from the web to the data layer and see what happens. If that solves the error then there are a couple ways to fix the issue.

Being that the Web project is also considered the Composition Root, it needs to provide configuration to the data layer such as the connection string. So there's already some knowledge shared between the web and data layers. Creating truly separate layers is nearly if not impossible.

If adding a hard dependency fixes the problem, then you can create a static method just for this purpose and document the method so future developers won't try and remove it. The other is to modify the solution file to copy the target dll into the bin folder. This seems like a better fix but I remember that it often broke and I had to keep going back and adding the manual entry back to the solution, which was confusing when it broke.

I also found another stackoverflow discussion that covers this same topic:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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