Reputation: 99
I'm setting up a project and i'm separating the layers into Presentation,Business and Data.
Presentation references Business Data references Business Business knows only about it self
For the Presentation layer I am using MVC and for the data layer I am using Entity Framework. Whilst using the MVC application and I get the following error when hitting the db via the business layer using IOC
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
If I add the Entity Framework to the MVC project with nuget all is well and works fine. However, it is my intention to separate layers and so doing this seems strange.
Do you have any ideas on how to get around this issue?
Thanks
Upvotes: 2
Views: 840
Reputation: 239220
I think you're confusing separation of layers with project dependencies. If you want all your data related stuff in your data project, that's fine. You can keep your context there, your migrations, etc. However, if other projects need to use that data layer, then they need Entity Framework installed to enable that communication. That doesn't mean that you're bleeding data layer code, it's just a simple dependency.
Upvotes: 0
Reputation: 7073
Firstly your Mvc project must have configuration for EF.
Example:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
</configuration>
And then
<connectionStrings>
<add name="BlogContext"
connectionString="metadata=res://*/BloggingModel.csdl|
res://*/BloggingModel.ssdl|
res://*/BloggingModel.msl;
provider=System.Data.SqlClient
provider connection string=
"data source=(localdb)\v11.0;
initial catalog=Blogging;
integrated security=True;
multipleactiveresultsets=True;""
providerName="System.Data.EntityClient" />
</connectionStrings>
Check this here... Referencing to the EntityFramework.dll cause an run-time error in VS2010
Upvotes: 1
Reputation: 7476
If you're using a single project to hold all these layers together, there is no other way. The Entity Framework configuration must be specified for the project in which it is used. However, this does not necessarily break your targeted layered structure. Although the Presentation layer could in fact access the data directly and not through the Data layer, you can design it not to, i.e. always get the data using the data provider (use a UnitOfWork for example).
An alternative would be to create separate applications/projects, for each layer. Your Data layer would be a "stand-alone" project, having it's own configuration. Your Presentation layer would then reference the Data layer project and just communicate with it trough the methods it provides, in order to exchange the data.
Upvotes: 2