【问题标题】:Linq Entity Exception not printing errorLinq 实体异常不打印错误
【发布时间】:2023-03-08 05:00:01
【问题描述】:

我是从数据库输入信息的 LINQ。我设置了 try.catch 块来捕获这些异常。但是我相信我遇到了一个痛点,我试图查看消息是什么,但它只是绕过向我打印消息并直接进入错误页面。这是我到目前为止的代码示例。我很想得到一些关于为什么这看起来如此奇怪的意见。

 private void CreateEntry()
    {
        var date = DateTime.Today;
        var version = (from v in house.StayLateVersions
                       where v.Active
                       select v).FirstOrDefault();

        if (version == null)
        {
            throw new NullReferenceException();
        }

        //Try to create an entry for the database.  Upon failure, sends the exception to ThrowDbError();

        try
        {
            ResidenceHallInspection rhi = new ResidenceHallInspection();

            rhi.versionId = version.id;
            rhi.submitDate = DateTime.Now;
            rhi.CheckInOrOut = ddlCheck.SelectedItem.Text;
            rhi.Id = txtId.Text;
            rhi.FirstName = txtFirstName.Text;
            rhi.MiddleName = txtMiddleName.Text;
            rhi.LastName = txtLastName.Text;
            rhi.Walls = chbxWalls.SelectedItem.Text;
            rhi.Windows = chbxWindows.SelectedItem.Text;
            rhi.Blinds = chbxBlinds.SelectedItem.Text;
            rhi.Couch = chbxCouch.SelectedItem.Text;
            rhi.CommonRoomCouch = chbxCRCouch.SelectedItem.Text;
            rhi.CommonRoomChair = chbxCRChair.SelectedItem.Text;
            rhi.Doors = chbxDoors.SelectedItem.Text;
            rhi.Carpet = chbxCarpet.SelectedItem.Text;
            rhi.Ceiling = chbxCeiling.SelectedItem.Text;
            rhi.CommonRoomCounter = chbxCRCounter.SelectedItem.Text;
            rhi.Cabinet = chbxCabinet.SelectedItem.Text;
            rhi.Phone = chbxPhone.SelectedItem.Text;
            rhi.Bed = chbxBed.SelectedItem.Text;
            rhi.Desk = chbxDesk.SelectedItem.Text;
            rhi.DeskChairs = chbxDeskChair.SelectedItem.Text;
            rhi.Tub = chbxTub.SelectedItem.Text;
            rhi.Vanity = chbxVanity.SelectedItem.Text;
            rhi.Notes = txtNotes.Text;
            rhi.Building = txtResHall.Text;
            rhi.ApartmentNumber = txtSuitNo.Text;
            rhi.BedSpace = txtBedSpace.Text;

            house.AddToResidenceHallInspections(rhi);
            house.SaveChanges();

        }
        catch (Exception oe)
        {
            ThrowDbError(oe);
            Response.Write(oe.InnerException);
        }
    }

    /*=================================================*/
    /*Possible Errors                                  */
    /*=================================================*/

    private void ThrowDbError(Exception oe)
    {
        Response.Write(oe.Source);
        house.Dispose();
        Session.Contents.Add("FormException", oe);
        Response.Redirect("/Database-Error/", true);
    }

【问题讨论】:

    标签: c# asp.net linq


    【解决方案1】:

    发生这种情况的最可能原因是您在 try/catch 块之外运行数据库 version 查询。您上面显示的代码不会处理此数据库访问代码中的任何异常。

    尝试扩展您的 try 块以包括数据库访问代码:

    var version = (from v in house.StayLateVersions
                       where v.Active
                       select v).FirstOrDefault();
    
    if (version == null)
    {
        throw new NullReferenceException();
    }
    

    看看这次错误是否被捕获。

    【讨论】:

    • 您可以尝试在ThrowDbError(oe); 行的catch 块中调试并设置断点吗?我感觉错误被捕获了,但是ThrowDbError 做了重定向并且错误没有显示出来。
    • 在我的本地机器上它运行良好,但是当它上传并实时运行时,问题就出现了
    • @Robert purpose 当发生错误时,您在Response.Redirect 之后执行Response.Write 也可能存在问题。尝试将消息记录到文件/数据库而不是执行 Response.Write
    • 似乎没有发送错误。有没有办法在服务器上找到这个?
    • 您可以尝试在全局级别处理错误(例如:在 globals.asax 中)。在此之前,请检查您在服务器上是否有正确的二进制文件(可能进行重建+重新部署)
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2022-01-23
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2011-08-19
    • 2013-08-20
    • 1970-01-01
    相关资源
    最近更新 更多