The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
Asked Answered
G

10

16

I have two projects in a solution.

  1. PizzaSoftware.Data
  2. PizzaSoftware.UI

In the Data project, I have my Entity Framework model which connects to my database.

My UI project has a project reference to Data and here's how it looks like:

enter image description here

I've created a user control in the UserControls folder.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PizzaSoftware.Data;

namespace PizzaSoftware.UI.UserControls
{
    public partial class AutoCompleteTextBox : UserControl
    {
        AutoCompleteStringCollection completeCollection = new AutoCompleteStringCollection();

        public AutoCompleteTextBox()
        {
            InitializeComponent();
        }

        private void AutoCompleteTextBox_Load(object sender, EventArgs e)
        {
            CustomerRepository repo = new CustomerRepository();
            var customers = repo.FindAllCustomers().ToList();

            foreach (var customer in customers)
            {
                completeCollection.Add(customer.Name);
            }

            txtSearchBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtSearchBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtSearchBox.AutoCompleteCustomSource = completeCollection;
        }
    }
}

When I try to drag this user control into the design pane, I receive the error in the question title.

Here's what my connection string looks like:

<connectionStrings>
   <add 
      name="SaharaPizzaEntities"
      connectionString="
         metadata=res://*/PizzaSoftwareEntityModel.csdl|res://*/PizzaSoftwareEntityModel.ssdl|res://*/PizzaSoftwareEntityModel.msl;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
            Data Source=.\SQLEXPRESS;
            Initial Catalog=SaharaPizza;
            Integrated Security=True;
            MultipleActiveResultSets=True
         &quot;"
      providerName="System.Data.EntityClient"
/>

What could be causing this error?

Gizela answered 27/3, 2011 at 17:28 Comment(1)
To clarify, the contents of App.config is the same on both projects.Gizela
V
11

In your app.config, your connection string look like..

   connection string=&quot;
      Data Source=.\SQLEXPRESS;
      Initial Catalog=SaharaPizza;
      Integrated Security=True;
      MultipleActiveResultSets=True
   &quot;

Notice the &quot. Try changing that to a single quote '

Vaillancourt answered 4/4, 2011 at 12:58 Comment(3)
I have same error message in a project, and changing that did not help. It is a testing/side project so if I have a solution I will be sure to leave a comment to help someone else.Syncrisis
Ok, I just fixed my project. It always compiled fine, the problem with mine was from a unit test project which needed me to add the same connection string to the App.Config file in the unit test project. All is fixed for me. I still have &quot; and that works fine for me, but I have seen msdn site where that single quote (') fixed it for them. Either way.... not a "fun" error as it is too vague. Oh wellSyncrisis
like tom said, I added the same connection string from my app.config to my web.config.Thallus
M
19

Copy <connectionStrings> from App.Config from PizzaSoftware.Data to web.config from PizzaSoftware.UI and add to web.config

<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
Moorfowl answered 21/5, 2012 at 20:59 Comment(1)
Albert Tolokonnikov's Solution worked for me, I Copied the connection strings in the web.config of my entity framework project to the web.config of my running project.Fantoccini
V
11

In your app.config, your connection string look like..

   connection string=&quot;
      Data Source=.\SQLEXPRESS;
      Initial Catalog=SaharaPizza;
      Integrated Security=True;
      MultipleActiveResultSets=True
   &quot;

Notice the &quot. Try changing that to a single quote '

Vaillancourt answered 4/4, 2011 at 12:58 Comment(3)
I have same error message in a project, and changing that did not help. It is a testing/side project so if I have a solution I will be sure to leave a comment to help someone else.Syncrisis
Ok, I just fixed my project. It always compiled fine, the problem with mine was from a unit test project which needed me to add the same connection string to the App.Config file in the unit test project. All is fixed for me. I still have &quot; and that works fine for me, but I have seen msdn site where that single quote (') fixed it for them. Either way.... not a "fun" error as it is too vague. Oh wellSyncrisis
like tom said, I added the same connection string from my app.config to my web.config.Thallus
M
2

I just found that if virtual directory for an app is created in IIS from VS2010 two levels from the website root this error would occur. Not sure why it happens, would need to investigate more. For example, if your app is in this path: /admin/advertiser the error would appear if you don't have /admin virtual directory in your IIS site.

All I did is created an empty admin directory in my .../intepub/wwwroot and error disappeared.

You will find that you won't be able to start debugging until you do the step above.

We had this problem in our team in past, it took some time to remember but this was exactly how we fixed it before also.

Maggiemaggio answered 18/7, 2012 at 17:46 Comment(1)
I just happened to have a VS 2010 project open to look at some source code when this happened. Closing it made the error with VS 2015 go away. That's messed up.Fleisher
F
0

The connection string looks like a valid string for the EntityClient provider, so my guess would be from the exception message that "the specified named connection is not found in the configuration".

The name of the connection string in the configuration is "SaharaPizzaEntities". Did you specify the "named connection" explicitely when you create your derived object context?

The generated object context class has several constructors, one is parameterless:

public EntityModelContainer() : base("name=EntityModelContainer",
    "EntityModelContainer")

name=EntityModelContainer is the connection string name which must match to the connection string in your configuration file ("SaharaPizzaEntities"). You could either change the name in the config file or use the second constructor which allows to define the connection string name explicitely:

public EntityModelContainer(string connectionString) : base(connectionString,
    "EntityModelContainer")
Foodstuff answered 27/3, 2011 at 18:19 Comment(0)
T
0

I had the same problem, so I was using the 2 thread like you and I thought that was because in the library doesn't run, then I put it in the principal project and then begin it to work completed successfully. This could be one solution, not what you need but you could resolve with this

please excuse my english.

I hope this can help you

Tipple answered 19/4, 2012 at 3:16 Comment(0)
P
0

The problem can easily be solved. Just copy your connection string from Aap.Config to Web.config file, this will surely run your apps. This works properly for me.
The error occurs when you have dataclass/entity in another Project and WebPages in a separate project.

Partlow answered 4/8, 2012 at 20:44 Comment(0)
V
0

I was also facing the same problem in my project. I was having 3 different projects

  1. Data in which I added the entity framework. this project was having the App.config file with the connection string
  2. Presenter: with my Presenter
  3. View: with UI.

I just copied the Connection string in the app config of View and it works fine.

and the reason of the problem was """Connection string not available locally, where it was required""".

Venturesome answered 2/2, 2013 at 6:13 Comment(0)
T
0

Here is a sample connection string which was not working

my error giving connection string was

  <connectionStrings>
<add name="IEMRWEBSEntities" connectionString="metadata=res://*/IemrWebs.csdl|res://*/IemrWebs.ssdl|res://*/IemrWebs.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.0.25;initial catalog=IEMRWEBSSitecore_Custom;persist security info=True;user id=IEMRWEBS;password=aDn16s!$AaS;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Problem is with path

see the * used in the connection string

"metadata=res://*/IemrWebs.csdl|res://*/IemrWebs.ssdl|res://*

replace * with the namespace in which your edmx file is placed

place the namespace instead of * in your appconfig file

my namespace of edmx file is IemrWebs.Data.Model so i have replaced * with IemrWebs.Data.Model see below and it is working Below is the correct connection string

<connectionStrings>
<add name="IEMRWEBSEntities" connectionString="metadata=res://IemrWebs.Data.Model/IemrWebs.csdl|res://IemrWebs.Data.Model/IemrWebs.ssdl|res://IemrWebs.Data.Model/IemrWebs.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.0.25;initial catalog=IEMRWEBSSitecore_Custom;persist security info=True;user id=IEMRWEBS;password=aDn16s!$AaS;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Taluk answered 7/11, 2013 at 14:7 Comment(0)
U
0

I got same problem & i tried all the mentioned method. finally i solved it as mentioned. In my case I have separate data layer and presentation layer. in my app.config (data layer) I have connection like this.

<add name="LibraryMgtSysEntities" connectionString="metadata=res://*/DataLibraryMgtSys.csdl|res://*/DataLibraryMgtSys.ssdl|res://*/DataLibraryMgtSys.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=abc;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=123;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

in my web.config i manually configured connection as follows:

<add name="DefaultConnection" providerName="System.Data.SqlClient"
 connectionString="Data Source=abc;
 Initial Catalog=LibraryMgtSys;
 Integrated Security=SSPI;
 user id=sa;password=123;" />

it gives me same exception as mentioned above. so i solved it by adding app.config value in web config file.

my final web.config file as follows:

<connectionStrings>
    <clear />
    <add name="LibraryMgtSysEntities" connectionString="metadata=res://*/DataLibraryMgtSys.csdl|res://*/DataLibraryMgtSys.ssdl|res://*/DataLibraryMgtSys.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=TILANITHOTAMUNE\SQLEXPRESS;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=testing;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" providerName="System.Data.SqlClient"
         connectionString="Data Source=abc;
         Initial Catalog=LibraryMgtSys;
         Integrated Security=SSPI;
         user id=sa;password=123;" />
  </connectionStrings>
Undershorts answered 13/11, 2013 at 19:27 Comment(0)
H
0

I think your config file is not in the web project, it is in some other DLL....not so sure...but your connection string should be in the web.config of project being executed...

Handbreadth answered 23/4, 2014 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.