Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid references to Entity framework in multiple projects

I want to encapsulate Entity Framework in one project. This project will do DB access alone. When updating, I want to map a domain model to EF. The purpose is that other layers of the solution should have no knowledge of infrastructure.

My problem is that I need to reference EF in my "caller" project to make it work.

Is there a way to avoid this?

Solution - ConsoleProject - EntityFrameworkProject (EF from Nuget)

like image 559
redlaz Avatar asked Sep 20 '26 02:09

redlaz


1 Answers

With EF6 and code based configurations, this is now possible.

[DbConfigurationType(typeof(MyDbContextConfiguration))]
internal class MyDbContext : DbContext
{
    public DbSet<MyModel> MyModels { get; set; }
}

internal class MyDbContextConfiguration : DbConfiguration
{
    public MyDbContextConfiguration()
    {
        SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
    }
}

If you configure your database in Project A as above, then only Project A needs to reference EntityFramework, and any project that references it, like Project B will not try to find the provider in its own assembly.

Expose your repositories in Project A, and add a <connectionStrings> configuration in Project B, and it should work flawlessly.

like image 68
René Sackers Avatar answered Sep 21 '26 17:09

René Sackers