简体   繁体   English

指定的命名连接在配置中找不到,不打算与EntityClient提供程序一起使用,或者无效

[英]The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

I have two projects in a solution. 我在解决方案中有两个项目。

  1. PizzaSoftware.Data PizzaSoftware.Data
  2. PizzaSoftware.UI PizzaSoftware.UI

In the Data project, I have my Entity Framework model which connects to my database. 在Data项目中,我有我的Entity Framework模型,它连接到我的数据库。

My UI project has a project reference to Data and here's how it looks like: 我的UI项目有一个Data的项目引用,这里是它的样子:

在此输入图像描述

I've created a user control in the UserControls folder. 我在UserControls文件夹中创建了一个用户控件。

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? 可能导致此错误的原因是什么?

Copy <connectionStrings> from App.Config from PizzaSoftware.Data to web.config from PizzaSoftware.UI and add to web.config 从PizzaSoftware.Data将App.Config中的<connectionStrings>从PizzaSoftware.Data复制到web.config并添加到web.config

<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>

In your app.config, your connection string look like.. 在您的app.config中,您的连接字符串看起来像..

   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 ' 尝试将其更改为单引号'

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. 我刚刚发现,如果应用程序的虚拟目录是从VS2010在IIS中从网站根目录创建两个级别,则会发生此错误。 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. 例如,如果您的应用程序位于此路径中: /admin/advertiser如果您的IIS站点中没有/admin虚拟目录,则会出现错误。

All I did is created an empty admin directory in my .../intepub/wwwroot and error disappeared. 我所做的只是在我的.../intepub/wwwroot创建了一个空的admin目录,错误消失了。

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. 我们过去在团队中遇到过这个问题,需要花一些时间才能记住,但这正是我们之前修复它的方式。

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. 我遇到了同样的问题,所以我正在使用像您这样的2线程,我认为这是因为在库中没有运行,然后我把它放在主项目中,然后开始工作成功完成。 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 我希望这可以帮到你

The problem can easily be solved. 问题很容易解决。 Just copy your connection string from Aap.Config to Web.config file, this will surely run your apps. 只需将您的连接字符串从Aap.Config复制到Web.config文件,这肯定会运行您的应用程序。 This works properly for me. 这适合我。
The error occurs when you have dataclass/entity in another Project and WebPages in a separate project. 当您在另一个项目中的数据类/实体和单独项目中的WebPages时,会发生此错误。

I was also facing the same problem in my project. 我的项目也遇到了同样的问题。 I was having 3 different projects 我有3个不同的项目

  1. Data in which I added the entity framework. 我在其中添加了实体框架的数据。 this project was having the App.config file with the connection string 这个项目有App.config文件和连接字符串
  2. Presenter: with my Presenter 演示者:与我的演示者
  3. View: with UI. 查看:使用UI。

I just copied the Connection string in the app config of View and it works fine. 我刚刚在View的app配置中复制了Connection字符串,它运行正常。

and the reason of the problem was """Connection string not available locally, where it was required""". 并且问题的原因是“”“连接字符串在本地不可用,需要”“”。

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 将*替换为放置edmx文件的命名空间

place the namespace instead of * in your appconfig file 在您的appconfig文件中放置名称空间而不是*

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 我的edmx文件的命名空间是IemrWebs.Data.Model所以我用IemrWebs.Data.Model替换了*,见下面它正在工作下面是正确的连接字符串

<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" />

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. 在我的app.config(数据层)我有这样的连接。

<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: 在我的web.config我手动配置连接如下:

<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. 所以我通过在web配置文件中添加app.config值来解决它。

my final web.config file as follows: 我的最终web.config文件如下:

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

我认为您的配置文件不在Web项目中,它在其他DLL中....不太确定...但您的连接字符串应该在正在执行的项目的web.config中...

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". 连接字符串看起来像是EntityClient提供程序的有效字符串,因此我的猜测来自异常消息“在配置中找不到指定的命名连接”。

The name of the connection string in the configuration is "SaharaPizzaEntities". 配置中的连接字符串的名称是“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"). name=EntityModelContainer是连接字符串名称,必须与配置文件中的连接字符串匹配(“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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载WPF设计器时出现异常“指定的命名连接…与EntityClient提供程序,否则无效。” - Exception “The specified named connection… with the EntityClient provider, or not valid.” while loading WPF designer CodeFirst:在配置中找不到指定的命名连接 - CodeFirst:The specified named connection is either not found in the configuration 在配置中找不到指定的命名连接 - The specified named connection is either not found in the configuration 在LocalDB + EntityFramework的配置错误中找不到指定的命名连接 - The specified named connection is either not found in the configuration error with LocalDB+EntityFramework 使用EF的本地数据库副本 - “在配置中找不到指定的命名连接” - Using local DB copy for EF - “The specified named connection is either not found in the configuration” ASP.Net C#:在配置中找不到指定的命名连接 - ASP.Net c#: The specified named connection is either not found in the configuration Edmx .net 4.0找不到指定的命名连接 - Edmx .net 4.0 The specified named connection is either not found 带有oracle的EF-“在配置中找不到指定的存储提供程序,或者它无效。” - EF with oracle - “The specified store provider cannot be found in the configuration, or is not valid.” EntityFramework,在配置中找不到指定的商店提供商,或者无效 - EntityFramework,The specified store provider cannot be found in the configuration, or is not valid 使用了命名连接字符串,但在应用程序的配置中找不到名称“DefaultConnection” - A named connection string was used, but the name 'DefaultConnection' was not found in the application's configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM