Linq实体异常未打印错误

本文关键字:打印 错误 异常 实体 Linq | 更新日期: 2023-09-27 18:00:57

我是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);
    }

Linq实体异常未打印错误

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

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

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

看看这次是否发现了错误。