How to avoid having to refer to EF?

0

Hello, everyone.

I basically have a data access layer with EF6 and a presentation layer (Asp.Net WebApi). I am not able to make it work without the presentation layer referring to EF. Does anyone know how to solve this?

My solution is currently like this:

Core       - sem dependências
Domain     - depende da Core
DataAccess - depende da Core, Domain e EF
WebApi     - depende da Core, Domain e DataAccess (e EF, mas não deveria)

There is no public class in DataAccess that exposes some EF dependency (DbContext, DbSet, etc). Instead, there is a set of own façade classes.

Without any reference, this exception occurs in the first line of code that accesses the data:

[System.InvalidOperationException]
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.
See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

Adding the EF section in Web.config (but without EF referencing), this other exception occurs immediately in DbContext instantiation:

[System.InvalidOperationException]
The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
registered in the application config file for the ADO.NET provider with invariant name
'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is
used and that the assembly is available to the running application.
See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
    
asked by anonymous 16.01.2018 / 18:51

1 answer

0

Finally, I came up with a solution that suits the scenario.

The cause

As Entity Framework 6.x depends on a configuration section in .config for definition of the access provider, which in turn is typed with classes in the library itself, is required in all subsequent layers of the solution.

<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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
</entityFramework>

The solution

I've decided to migrate to the EF Core for seeing that the provider settings and all other settings do not are defined in configuration files, which enables greater uncoupling between layers. Then I just needed to write encapsulation classes of the data access features I want to expose (DataService layer).

I created a repository in GitHub with a very simplified prototype of this architecture.

    
01.02.2018 / 15:02