Where should I put Connectionstring in Windows Forms Applications with EF?

Asked

Viewed 518 times

2

I have the following error using Entity Framework:

Additional information: 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. See http://go.microsoft.com/fwlink/? Linkid=260882 for more information.

From what I’ve researched EPH couldn’t find my ConnectionString and uses the standard

Data Source=.\\SQLEXPRESS;Initial Catalog=ResenhaInformalContext;Integrated Security=True;MultipleActiveResultSets=True

I’m wearing a WINDOWS FORMS APPLICATION and a CLASS LIBRARY of infrastructure already put the ConnectionString in App.Config, both in the Class Library where the Entity Framework is installed, and in the W.F where the start of the application takes place.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ResenhaInformalContext" connectionString="Data Source=RENAN-   NB\ESPACOCULTURA;Initial Catalog=ResenhaInformalContext;Persist Security     Info=True;User ID=sa;Password=renan010203;MultipleActiveResultSets=True"      providerName="System.Data.SqlClient" />
</connectionStrings>

How to resolve this error ?

  • 1

    In the Package Manager Console execute the following command: Install-Package EntityFramework

  • I have it installed in the infrastructure layer... You say I should also install in the WF application?

  • Vc added the EF reference to your project?

  • This is usually because the EF not installed right, the above command is to reinstall it correctly.

1 answer

2

You need to have the Entity Framework referenced also in your main project.

Importantly, the Connection string used will be the one in the app.config (or web.config) of the main project - this also holds true for running Migrations, the Connection string will always be of the project that is marked as startup project.

  • EF does not need to be referenced. It can only be in the Infrastructure Class Library. It would be nice to put an example of Web.config for him.

  • Are you sure? So I just need to have the configSections in the main project?

  • Exactly. The reading of Config Section can be done only by Class Library.

  • Let me just confirm, I need to have the EF section (and configSections, of course) in the app.config of the class library and in the main project I only need the Connection string?

  • Exactly. Try creating a project with DLL Models separate.

  • I solved the error as follows: I put the code in my context class. public ResenhaInformalContext()&#xA; : base("ResenhaInformalContext")&#xA; {&#xA; var chamada = System.Data.Entity.SqlServer.SqlProviderServices.Instance;&#xA; } I saw this in another example of this error with a Console Application project. I tried it and it worked. But I don’t know what he does if someone can explain to me.

  • I saw the @jbueno comment, and I tested it on my show. Before I had two app.config, one in the Class Library and one in the main project, because I thought the project used both files. I commented on the Class Library app.config leaving it only in the main project and worked normally.

  • @Ciganomorrisonmendez, you spoke in the first comment that the EF does not need to be referenced in the main project, but today moving a parade exactly like this one, only worked when referenced in the main project. in some idea of what might be?

  • @Renanoliveira Ask a new question, please.

Show 4 more comments

Browser other questions tagged

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