Why is compiler requesting reference to Entity Framework in User Interface project?

c# entity-framework-6 winforms


I am trying to learn how to create a Winform C# app, that uses EF with repository pattern.

From my understanding, UI layer should be independent from Entity Framework (it uses data through repository and it shouldn't care if there is an Entity framework or any other data provider), but compiler is showing and error if WinForms project is not having reference to Entity framework (when I add reference to EF, everything works fine)

Error message

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

Why compiler needs my Winform project to have reference to Entity framework?

Code from Winforms project

    private void MainForm_Load(object sender, EventArgs e)
        //Customer is a POCO class
        using (var customers = new EfRepository<Customer>())
            foreach (var c in customers.All())

    private void SaveCustomerButton_Click(object sender, EventArgs e)
        var newCustomer=new Customer();
        newCustomer.CustomerName = CName.Text;
        newCustomer.CustomerDescription = CDescription.Text;
        using (var customers = new EfRepository<Customer>())

Code from Repository project

public class EfRepository<T> : IRepository<T>, IDisposable where T : class//, IEntity<int>
    private FirstContext _ctx = new FirstContext();
    private DbSet<T> _set;

    public EfRepository()
        //_ctx = _context;
        _set = _ctx.Set<T>();
    public void Add(T newEntity)
    public IQueryable<T> All()
        return _set;


public class FirstContext : BaseContext<FirstContext>
    public DbSet<Customer> Customers{ get; set; }


I added app.config (to my UI project) with information about my database, but it works only if I add Entity Framework to my UI project, if I remove EF from my UI project repository that connects to database is not returning any data (no error as well...)

Here is content of my app.config:

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <add name="RepositoryDb" connectionString="Data Source=.;Initial Catalog=RepositoryDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
10/20/2013 12:46:13 PM

Accepted Answer

You are right that your GUI projects should not need the EF assemblies directly. It will off course still load them through your Repository project.

But an application uses, by default, only 1 config file.
So you do need the db settings in app.config, like the error is telling you.

When you added the EF libs, there were probably a few changes in the config file. Maybe it'll keep working when you remove the libs again.

10/19/2013 8:14:58 AM

Popular Answer

After putting the config file in the main app, this was what solved my problem

 public PSContext() : base() 

            var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

No more errors...

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow