Entity Framework 6: Provider Error

Asked

Viewed 210 times

2

I am using EF6 Code First. While performing "Update-Database" the database is normally created. But when entering the data the following error is launched:

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.

On 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 setting below that I saw in a tutorial from Microsoft, but with it the program didn’t 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 are with this same configuration and had no problem.

  • puts your connection class to see how to pass in the constructor , ta doing in separate layer ?

  • I put Eduardo. Yes I’m using it in separate layers with Repository Pattern.

  • @Raphael , what is the need to use Repository Pattern?

  • Hello Renan. It would be to leave isolated the layer of business rules of the bank layer. (Separate responsibilities).

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 the Linker removes all references you think are not used at the time of execution of Seed.

This line forces an assignment, and therefore prevents the Linker to remove the reference by default.

Place your class you inherit from DBContext thus:

public class AppContext : DbContext
{
    public AppContext()
        : base("StringSqlServerConnection")
    { var chamada = System.Data.Entity.SqlServer.SqlProviderServices.Instance;  }
    ...
}
  • Thanks Renan, but what I do with the return of this instance?

  • @Raphael, I made an issue. take the test .

  • Thanks Renan, it worked! I just found this strange because you are calling an instance and passing it to a variable and you are not doing anything else hehe.

  • 1

    @Raphael edited the answer. Now with the explanation of what the code does.

0

The project where it is mapped in webconfig the Connection string must be set as main and use providerName="System.Data.SqlClient" or do with ConfigurationManager.ConnectionStrings["teste"].ConnectionString the times I’ve been through this problem I’ve solved it this way

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

            }     

        }
  • Your answers are very unformatted.

  • I did the same way (Configurationmanager.Conn...) and gave the same error. And how do I set the string Connection as main? Maybe that could be it.

  • right click on the set project As Startup project

  • Ah yes, it was the first thing I had done hehe.

Browser other questions tagged

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