Entity Framework 6: Error in Provider

2

I'm using EF6 Code First. When performing the "Update-Database" the bank is created normally. But entering the data throws the following error:

  

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.

In Web.config I put the connection string with the provider:

<connectionStrings>
    <add name="StringSqlServerConnection" connectionString="Data Source=NOTE-RAPHAEL\SQLEXPRESS;Initial Catalog=ProjetoTesteDb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

I put this configuration below that I saw in a tutorial from Microsoft, but with that the program did not even open:

<configSections> 
    <section name="entityFramework" 
        type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
</configSections> 

Connection class:

public class AppContext : DbContext
{
    public AppContext()
        : base("StringSqlServerConnection")
    { }

    ...
}

I have other projects that have the same configuration and I had no problem.

    
asked by anonymous 29.11.2016 / 15:28

2 answers

4

This is due to the fact that, although you are using a reference in the project, the reference is never used, and linker removes all references that you think are not used at the time of Seed .

This line forces an assignment, and therefore prevents the linker from removing the reference by default.

Place your class that inherits from DBContext like this:

public class AppContext : DbContext
{
    public AppContext()
        : base("StringSqlServerConnection")
    { var chamada = System.Data.Entity.SqlServer.SqlProviderServices.Instance;  }
    ...
}
    
30.11.2016 / 20:33
0

The project where it is mapped in webconfig, the connection string must be set to principal and use providerName="System.Data.SqlClient "or to do with ConfigurationManager.ConnectionStrings["teste"].ConnectionString the times I went through this problem I solved this way

   public class Conexao : DbContext
        {
            public Conexao()
                : base(ConfigurationManager.ConnectionStrings["teste"].ConnectionString)
            {

            }     

        }
    
29.11.2016 / 15:42