为什么 Database.SqlQuery 返回 0 行?

Why Database.SqlQuery is returning 0 rows?

我用过这个

        public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
        {
            var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails", InspectionReportID).ToList();

            return List;
        }

在继承自 DbContext

的 class 中

我在控制器中调用此函数,但列表为空。 Database.SqlQuery returns 0 个项目,甚至程序正在为我提供给函数的参数返回数据,但不是这个。

更新:

alter PROCEDURE GetInspectionReportDetails 

    @InspectionReportID int=0

AS
BEGIN

    Select ir.InspectionReportID
          ,ir.VelosiProjectNo
          ,ir.VelosiReportNo   
          ,ir.Reference
          ,ir.PoNo
          ,ir.InspectionDate
          ,ir.IssueDate
          ,ir.InspectionPhase
          ,ir.InServiceInspection
          ,ir.NewInduction
          ,ir.HydrostaticTest
          ,ir.DimensionalCheck
          ,ir.ThicknessCheck
          ,ir.Patrom
          ,ir.Gvs
          ,ir.FinalOgraInspection
          ,ir.OmcClientRequirement
          ,ir.TankLorryRegistrationNo
          ,ir.TruckTractorManufacturerName
          ,ir.ClientName
          ,ir.Capacity
          ,ir.Omc
          ,ir.EngineNo
          ,ir.TankLorryDimension
          ,ir.ChassisNo
          ,ir.InspectionPlace
          ,ir.TankLorryEnginePower
          ,ir.CarriageName
          ,ir.Brakes
          ,ir.IsSatisfactory
          ,ir.Remarks
          ,ir.Rev
          ,ir.Description
          ,ir.Status
          ,u1.UserName as PeparedBy
          ,u2.UserName as CheckedBy
          ,u3.UserName as ApprovedBy
          ,u4.UserName as IssuedBy
      From InspectionReport ir
      Inner Join dbo.[User] u1
      ON u1.UserID= ir.PeparedBy
      Inner Join dbo.[User] u2
      ON u2.UserID= ir.CheckedBy
      Inner Join dbo.[User] u3
      ON u3.UserID= ir.ApprovedBy
      Inner Join dbo.[User] u4
      ON u4.UserID= ir.IssuedBy

      where ir.InspectionReportID= @InspectionReportID

这是函数调用的存储过程。

这 class inpsectionreport;

namespace VAILCertificates.DAL.Entities
{
    public class InspectionReport
    {
        //General Details
        public int InspectionReportID { get; set; }

        [Required]
        [Display (Name= "Velosi Project No")]
        public string VelosiProjectNo { get; set; }

        [Display(Name = "Velosi Report No")]
        public string VelosiReportNo { get; set; }

        [Required]
        public string Reference { get; set; }

        [Required]
        [Display(Name = "PO No")]
        public string PoNo { get; set; }

        [Required]
        [Display(Name = "Inspection Date")]
        public DateTime InspectionDate { get; set; }

        [Required]
        [Display(Name = "Issue Date")]
        public DateTime IssueDate { get; set; }

        //Inspection Phase
        [Required]
        [Display(Name = "Inspection Phase")]
        public byte InspectionPhase { get; set; } //0= Before, 1= During, 2= Final

        //Types Of Inspection
        [Display(Name = "In Service Inspection")]
        public bool InServiceInspection { get; set; }

        [Display(Name = "New Induction")]
        public bool NewInduction { get; set; }

        [Display(Name = "Hydorstatic Test")]
        public bool HydrostaticTest { get; set; }

        [Display(Name = "Dimensional Check")]
        public bool DimensionalCheck { get; set; }

        [Display(Name = "Thickness Check")]
        public bool ThicknessCheck { get; set; }

        [Display(Name = "PATROM")]
        public bool Patrom { get; set; }

        [Display(Name = "GVS")]
        public bool Gvs { get; set; }

        [Display(Name = "Final OGRA Inspection")]
        public bool FinalOgraInspection { get; set; }

        [Display(Name = "OMC/Client Requirement")]
        public bool OmcClientRequirement { get; set; }

        //Details Of Tank Lorry

        [Required]
        [Display(Name = "Tank Lorry Registration Number")]
        public string TankLorryRegistrationNo { get; set; }

        [Required]
        [Display(Name = "Truck Tractor Manufacturer Name")]
        public string TruckTractorManufacturerName { get; set; }

        [Required]
        [Display(Name = "Client Name")]
        public string ClientName { get; set; }

        [Required]
        [Display(Name = "Capacity")]
        public string Capacity { get; set; }

        [Required]
        [Display(Name = "OMC")]
        public string Omc { get; set; }

        [Required]
        [Display(Name = "Engine No")]
        public string EngineNo { get; set; }

        [Required]
        [Display(Name = "Tank Lorry Dimension")]
        public string TankLorryDimension { get; set; }

        [Required]
        [Display(Name = "Chassis Number")]
        public string ChassisNo { get; set; }

        [Required]
        [Display(Name = "Inspection Place")]
        public string InspectionPlace { get; set; }

        [Required]
        [Display(Name = "Tank Lorry (Engine Power)")]
        public string TankLorryEnginePower { get; set; }

        [Required]
        [Display(Name = "Carriage Name")]
        public string CarriageName { get; set; }

        [Required]
        public string Brakes { get; set; }

        //ResultsofInspection

        [Required]
        [Display(Name = "Is Satisfactory?")]
        public bool IsSatisfactory { get; set; }

        //Remarks
        [Required]
        public string Remarks { get; set; }

        //ApprovalSection

        public string Rev { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public int PeparedBy { get; set; }

        public int CheckedBy { get; set; }
        public int ApprovedBy { get; set; }

        public int IssuedBy { get; set; }

        //ReportStatus

        public byte Status { get; set; } //0= Prepared 1= CheckedBy 2= ApprovedBy

    }
}

尝试更改 sql 语句,我觉得错误是你如何调用 sql 语句。

var list = this.Database.SqlQuery<InspectionReport>($"EXEC GetInspectionReportDetails {InspectionReportID}").ToList();

尝试使用此代码:

public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
{
    var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails @InspectionReportID", InspectionReportID).ToList();
    return List;
}

您还可以构建 DbParameter 并将其作为参数值提供给存储过程:

var InspectionReportID= new SqlParameter("@InspectionReportID", 0);

var list = this.Database.SqlQuery<InspectionReport>("EXECUTE dbo.GetInspectionReportDetails @InspectionReportID",InspectionReportID).ToList();

重要的是参数化任何用户输入以防止 SQL 注入攻击。您可以在 SQL 查询字符串中包含参数占位符,然后提供参数值作为附加参数。您提供的任何参数值都将自动转换为 DbParameter.