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


Question

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())
            {
                CList.Items.Add(c.CustomerName);
            }
        }
    }

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

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)
    {
        _set.Add(newEntity);
    }
    public IQueryable<T> All()
    {
        return _set;
    }

Context

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

[EDIT]

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:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="RepositoryDb" connectionString="Data Source=.;Initial Catalog=RepositoryDb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <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>
</configuration>

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.


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...





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why