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

发布于 2024-10-26 23:28:02 字数 2031 浏览 4 评论 0原文

我的解决方案中有两个项目。

  1. PizzaSoftware.Data
  2. PizzaSoftware.UI

在数据项目中,我有连接到数据库的实体框架模型。

我的 UI 项目有一个对数据的项目引用,如下所示:

在此处输入图像描述

我已创建一个用户控件位于 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;
        }
    }
}

当我尝试将此用户控件拖到设计窗格中时,我在问题标题中收到错误。

我的连接字符串如下所示:

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

是什么导致了此错误?

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="
            Data Source=.\SQLEXPRESS;
            Initial Catalog=SaharaPizza;
            Integrated Security=True;
            MultipleActiveResultSets=True
         ""
      providerName="System.Data.EntityClient"
/>

What could be causing this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

别再吹冷风 2024-11-02 23:28:02

从 App.Config 从 PizzaSoftware.Data 复制到 web.config
PizzaSoftware.UI 并添加到 web.config

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

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>
夏了南城 2024-11-02 23:28:02

在你的 app.config 中,你的连接字符串看起来像……

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

注意 "。尝试将其更改为单引号 '

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

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

Notice the ". Try changing that to a single quote '

清醇 2024-11-02 23:28:02

我刚刚发现,如果从 VS2010 的 IIS 中创建应用程序的虚拟目录,距网站根目录两层,就会发生此错误。不确定为什么会发生,需要进行更多调查。
例如,如果您的应用程序位于以下路径:/admin/advertiser,并且您的 IIS 站点中没有 /admin 虚拟目录,则会出现错误。

我所做的就是在我的 .../intepub/wwwroot 中创建一个空的 admin 目录,然后错误消失了。

您会发现,在执行上述步骤之前,您将无法开始调试。

我们的团队过去曾遇到过这个问题,需要一些时间才能记住,但这正是我们之前解决它的方法。

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.

爱她像谁 2024-11-02 23:28:02

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

配置中连接字符串的名称是“SaharaPizzaEntities”。当您创建派生对象上下文时,您是否明确指定了“命名连接”?

生成的对象上下文类有多个构造函数,其中一个是无参数的:

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

name=EntityModelContainer 是连接字符串名称,必须与配置文件中的连接字符串(“SaharaPizzaEntities”)匹配。您可以更改配置文件中的名称或使用第二个构造函数,它允许显式定义连接字符串名称:

public EntityModelContainer(string connectionString) : base(connectionString,
    "EntityModelContainer")

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")
岁月流歌 2024-11-02 23:28:02

我遇到了同样的问题,所以我像你一样使用 2 个线程,我认为这是因为库中没有运行,然后我将它放入主项目中,然后开始它成功完成工作。
这可能是一种解决方案,不是您需要的,但您可以解决这个问题,

请原谅我的英语。

我希望这可以帮助你

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

后eg是否自 2024-11-02 23:28:02

这个问题很容易解决。只需将您的连接字符串从 Aap.Config 复制到 Web.config 文件,这肯定会运行您的应用程序。这对我来说很有效。
当您在另一个项目中拥有数据类/实体并且在单独的项目中拥有网页时,会发生此错误。

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.

岁月打碎记忆 2024-11-02 23:28:02

我在项目中也遇到了同样的问题。
我有 3 个不同的项目

  1. 数据,我在其中添加了实体框架。该项目的 App.config 文件带有连接字符串
  2. Presenter: with my Presenter
  3. View: with UI。

我刚刚在 View 的应用程序配置中复制了连接字符串,它工作正常。

问题的原因是“”“连接字符串在本地不可用,而需要它””“。

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

一杆小烟枪 2024-11-02 23:28:02

这是一个不起作用的示例连接字符串

我给出连接字符串的错误是

  <connectionStrings>
<add name="IEMRWEBSEntities" connectionString="metadata=res://*/IemrWebs.csdl|res://*/IemrWebs.ssdl|res://*/IemrWebs.msl;provider=System.Data.SqlClient;provider connection string="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"" providerName="System.Data.EntityClient" />

问题是路径

请参阅连接字符串中使用的 *

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

将 * 替换为放置 edmx 文件的命名空间

将命名空间而不是 * 放置在您的 appconfig 文件中

我的 edmx 文件的命名空间是 IemrWebs.Data。模型
所以我用 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="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"" providerName="System.Data.EntityClient" />

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="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"" 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="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"" providerName="System.Data.EntityClient" />

下壹個目標 2024-11-02 23:28:02

我遇到了同样的问题&我尝试了所有提到的方法。最后我按照提到的解决了它。就我而言,我有单独的数据层和表示层。在我的 app.config (数据层)中,我有这样的连接。

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

在我的 web.config 中,我手动配置了连接,如下所示:

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

它给了我与上面提到的相同的异常。所以我通过在 Web 配置文件中添加 app.config 值来解决它。

我的最终 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="data source=TILANITHOTAMUNE\SQLEXPRESS;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=testing;MultipleActiveResultSets=True"" 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>

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="data source=abc;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=123;MultipleActiveResultSets=True"" 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="data source=TILANITHOTAMUNE\SQLEXPRESS;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=testing;MultipleActiveResultSets=True"" 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>
乖乖 2024-11-02 23:28:02

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文