How to avoid having to refer to the RU?

Asked

Viewed 105 times

0

Hello, everyone.

I basically have a data access layer with EF6 and a presentation layer (Asp.Net Webapi). I am unable 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 any EF dependency (Dbcontext, Dbset, etc). Instead, there is a set of own facade classes (Façade).

No reference occurs this exception in the first line of code that makes access to 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 on Web.config (but no reference to EF) this other exception occurs right at the 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.
  • Surely you are accessing the data layer directly without using the di (dependency Injection)

  • And how would I do that? Any example? The Dataaccess layer exposes no EF class.

  • http://desenvolvedor.ninja/inversao-de-controle-e-injecao-de-dependencia-no-asp-net-core/

  • http://desenvolvedor.ninja/dryioc-inversao-de-controle-e-injecao-de-dependencia-no-net/

  • I am not developing Asp.Net Core or EF Core. I am with Framework 4.7.1 and EF6.

  • this works for any version

  • As far as I know, it’s not possible. The running application (whether a Web API, a WPF, a Console, or even a test project) needs to have the EF installed (even using DI), and I don’t understand why that would be a problem.

Show 2 more comments

1 answer

0


Finally, I arrived at a solution that satisfies the scenario.

The cause

Since Entity Framework 6.x depends on a configuration section in . config for setting the access provider and this in turn is tipada with classes from the library itself, reference becomes necessary 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 decided to migrate to the EF Core for seeing that the settings of providers and all the others are not defined in configuration files, which makes possible a greater decoupling between layers. Hence it is only necessary to write encapsulation classes of the data access functionalities I want to expose (Dataservice layer).

I created a repository on Github with a very simplified prototype of this architecture.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.