实施事件时,为什么会出现无连接错误?

时间:2019-07-04 04:59:02

标签: c# winforms events mvp

我正在使用c#WinForms和Entity Framework创建一个桌面应用程序。我试图将我的观点与演示者与事件脱钩,并且当我这样做时会破坏某些东西。当我单击Form1.cs时收到错误消息“在应用程序配置文件中找不到名为'TipManagerDBEntities'的连接字符串”。

我检查了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=&quot;data source=DESKTOP-GNG12RP\SQLEXPRESS;initial catalog=TipManagerDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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本身时,不会出现此错误,但是在使用事件时,每次都会发生。知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

该事件与问题无关。连接字符串是问题所在。首先,我从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';")
    {
    }

老实说,我不知道为什么这可以解决问题。该程序仍然能够连接到数据库并查询数据,但是会出现错误。