为什么我在实施事件时会收到无连接错误?
Why am I Getting a no connection Error when I Implement an Event?
我正在使用 c# WinForms 和 Entity Framework 创建桌面应用程序。我试图通过事件将我的观点与演示者分离,当我这样做时它会破坏某些东西。单击 Form1.cs 时出现错误 "No connection string named 'TipManagerDBEntities' could be found in the application config file"。
我检查了 App.config 文件以确保连接字符串在其中,并确保配置文件加载了解决方案。在进行事件更改之前,我已将我的存储库重置为正确的状态,并且一切正常。然后我重写事件并且它运行良好。当我保存并重新打开项目时,出现错误。当我调试程序时,它仍然运行良好,但我似乎无法找到错误的根源。
这是错误的调用堆栈:
at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
at TipManager.Services.Repository.GetSumOfDeposits() in C:\Users\Adam\source\repos\TipManager\Services\Repository.cs:line 15
at TipManager.Services.TipManagerServices.passSumTotalToModel() in C:\Users\Adam\source\repos\TipManager\Services\TipManagerServices.cs:line 22
at TipManager.Presenter.HomePresenter.DisplayTotal() in C:\Users\Adam\source\repos\TipManager\Presenter\HomePresenter.cs:line 34
at TipManager.Presenter.HomePresenter.OnHomeLoaded(Object sender, EventArgs e) in C:\Users\Adam\source\repos\TipManager\Presenter\HomePresenter.cs:line 56
at TipManager.UserControls.Home.Home_Load(Object sender, EventArgs e) in C:\Users\Adam\source\repos\TipManager\UserControls\Home.cs:line 40
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
包含连接字符串的 App.config 文件:
<connectionStrings>
<add name="TipManagerDBEntities" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-GNG12RP\SQLEXPRESS;initial catalog=TipManagerDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
带有 IHome 界面的视图:
public partial class Home : UserControl, IHome
{
//private HomePresenter presenter;
public Home()
{
InitializeComponent();
new HomePresenter(this);
}
private void Home_Load(object sender, EventArgs e)
{
//presenter = new HomePresenter(this);
//presenter.DisplayTotal();
EventHandler handler = homeLoaded;
handler?.Invoke(this, e);
}
主持人:
class HomePresenter
{
TipManagerModel tipManager = new TipManagerModel();
TipManagerServices services;
private IHome homeView;
public HomePresenter(IHome view)
{
homeView = view;
services = new TipManagerServices(tipManager);
homeView.homeLoaded += new EventHandler(OnHomeLoaded);
}
public void OnHomeLoaded(object sender, EventArgs e)
{
DisplayTotal();
}
奇怪的是,一切仍按预期工作,但我收到一个很大的错误页面,阻止我的设计人员选择“忽略并继续”,但我想找到问题的根源。在视图中,当我使用注释掉的代码创建和调用 Presenter 本身时,我没有收到此错误,但是当我使用该事件时,每次都会发生这种情况。知道我做错了什么吗?
事件与问题无关。连接字符串是问题所在。首先,我从 app.config 文件中复制了连接字符串,不包括末尾的提供商名称部分,并将“"”更改为“'”。然后将字符串直接传递给上下文文件中的基本构造函数,如下所示:
public partial class TipManagerDBEntities : DbContext
{
public TipManagerDBEntities()
: base(@"metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=';data source=DESKTOP-GNG12RP\SQLEXPRESS;initial catalog=TipManagerDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';")
{
}
老实说,我不知道为什么这能解决问题。该程序仍然能够连接到数据库并查询数据,但您会收到错误消息。
我正在使用 c# WinForms 和 Entity Framework 创建桌面应用程序。我试图通过事件将我的观点与演示者分离,当我这样做时它会破坏某些东西。单击 Form1.cs 时出现错误 "No connection string named 'TipManagerDBEntities' could be found in the application config file"。
我检查了 App.config 文件以确保连接字符串在其中,并确保配置文件加载了解决方案。在进行事件更改之前,我已将我的存储库重置为正确的状态,并且一切正常。然后我重写事件并且它运行良好。当我保存并重新打开项目时,出现错误。当我调试程序时,它仍然运行良好,但我似乎无法找到错误的根源。
这是错误的调用堆栈:
at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
at TipManager.Services.Repository.GetSumOfDeposits() in C:\Users\Adam\source\repos\TipManager\Services\Repository.cs:line 15
at TipManager.Services.TipManagerServices.passSumTotalToModel() in C:\Users\Adam\source\repos\TipManager\Services\TipManagerServices.cs:line 22
at TipManager.Presenter.HomePresenter.DisplayTotal() in C:\Users\Adam\source\repos\TipManager\Presenter\HomePresenter.cs:line 34
at TipManager.Presenter.HomePresenter.OnHomeLoaded(Object sender, EventArgs e) in C:\Users\Adam\source\repos\TipManager\Presenter\HomePresenter.cs:line 56
at TipManager.UserControls.Home.Home_Load(Object sender, EventArgs e) in C:\Users\Adam\source\repos\TipManager\UserControls\Home.cs:line 40
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
包含连接字符串的 App.config 文件:
<connectionStrings>
<add name="TipManagerDBEntities" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-GNG12RP\SQLEXPRESS;initial catalog=TipManagerDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
带有 IHome 界面的视图:
public partial class Home : UserControl, IHome
{
//private HomePresenter presenter;
public Home()
{
InitializeComponent();
new HomePresenter(this);
}
private void Home_Load(object sender, EventArgs e)
{
//presenter = new HomePresenter(this);
//presenter.DisplayTotal();
EventHandler handler = homeLoaded;
handler?.Invoke(this, e);
}
主持人:
class HomePresenter
{
TipManagerModel tipManager = new TipManagerModel();
TipManagerServices services;
private IHome homeView;
public HomePresenter(IHome view)
{
homeView = view;
services = new TipManagerServices(tipManager);
homeView.homeLoaded += new EventHandler(OnHomeLoaded);
}
public void OnHomeLoaded(object sender, EventArgs e)
{
DisplayTotal();
}
奇怪的是,一切仍按预期工作,但我收到一个很大的错误页面,阻止我的设计人员选择“忽略并继续”,但我想找到问题的根源。在视图中,当我使用注释掉的代码创建和调用 Presenter 本身时,我没有收到此错误,但是当我使用该事件时,每次都会发生这种情况。知道我做错了什么吗?
事件与问题无关。连接字符串是问题所在。首先,我从 app.config 文件中复制了连接字符串,不包括末尾的提供商名称部分,并将“"”更改为“'”。然后将字符串直接传递给上下文文件中的基本构造函数,如下所示:
public partial class TipManagerDBEntities : DbContext
{
public TipManagerDBEntities()
: base(@"metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=';data source=DESKTOP-GNG12RP\SQLEXPRESS;initial catalog=TipManagerDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework';")
{
}
老实说,我不知道为什么这能解决问题。该程序仍然能够连接到数据库并查询数据,但您会收到错误消息。