为什么使用 dapper 向我的列表对象查询 returns 一些空值

Why Query returns some null values to my list Object using dapper

我有这个功能查询,出于安全考虑,我只使用了虚构的表名:

SELECT 
h.CENID,
 h.BMHFMC, 
 h.BMHDONEMIDASSTEP1, 
 h.BMHDONEMIDASSTEP2,
 h.LMIID, 
 h.BMHHOLD,
 h.BMHBATCHMIDAS,
 h.BMHFMCVALUEDATE AS HeaderValueDate, 
 h.SUNID, 
 h.BRAID,
 d.BMHID, 
 d.BMDRUBRIQUE,
 d.BMDCLIENT,
 d.BMDSEQUENCE,
 d.BMDDATE, 
 d.BMDDEVISE, 
 d.BMDMONTANT,
 d.BMDTYPE, 
 d.BMDNOTE, 
 d.BMDENTRYNBRE,
 v.DEVDECIMAL ,
 NVL(t.TYPVERIFCOMPTEMIDAS, 0) AS TYPVERIFCOMPTEMIDAS 
 FROM dbo.TableOne h 
 INNER JOIN dbo.Tabletwoo d
 ON h.BMHID = d.BMHID 
 INNER JOIN dbo.tableThree v
 ON d.BMDDEVISE = v.DEVID 
 LEFT JOIN dbo.TableFour t 
 ON  t.TYPID=h.BMHFMC  
 WHERE d.BMDMONTANT != 0 
 AND h.BMHDONEMIDASSTEP1 = 0
 AND h.BMHDONEMIDASSTEP2 = 0 
 AND h.LMIID = 0 
 AND h.BMHHOLD = 0 

我做了一个 class 来绑定每个字段

 public class Batch :BaseRepository ,IList<Batch>
    {
        public Batch()
        {
        }

        private string cendid;
        private string bmhfmc;
        private double bmhdonemidasstep1;
        private double bmhdonemidasstep2;
        private double lmiid;
        private double bmhhold;
        private double bmhbatchmidas;
        private DateTime headervaluedateordinal;
        private double sunid; // 
        private string bradid; //
        private double bmhid;
        private string bmdrubirique; //
        private string bmdclient;
        private string bmdsequence;
        private DateTime bmddate;
        private string bmddevise;
        private double bmdmontant;
        private string bmdtype;
        private string bmdnote;
        private string bmdentrynbre; //
        private double devdecimalordinal;
        private double typverifcomptemidasordinal;

        public Batch(string cendid, string bmhfmc, double bmhdonemidasstep1, double bmhdonemidasstep2, double lmiid, double bmhhold, double bmhbatchmidas, DateTime headervaluedateordinal, double sunid, string bradid, double bmhid, string bmdrubirique, string bmdclient, string bmdsequence, DateTime bmddate, string bmddevise, double bmdmontant, string bmdtype, string bmdnote, string bmdentrynbre, double devdecimalordinal, double typverifcomptemidasordinal)
        {
            this.cendid = cendid;
            this.bmhfmc = bmhfmc;
            this.bmhdonemidasstep1 = bmhdonemidasstep1;
            this.bmhdonemidasstep2 = bmhdonemidasstep2;
            this.lmiid = lmiid;
            this.bmhhold = bmhhold;
            this.bmhbatchmidas = bmhbatchmidas;
            this.headervaluedateordinal = headervaluedateordinal;
            this.sunid = sunid;
            this.bradid = bradid;
            this.bmhid = bmhid;
            this.bmdrubirique = bmdrubirique;
            this.bmdclient = bmdclient;
            this.bmdsequence = bmdsequence;
            this.bmddate = bmddate;
            this.bmddevise = bmddevise;
            this.bmdmontant = bmdmontant;
            this.bmdtype = bmdtype;
            this.bmdnote = bmdnote;
            this.bmdentrynbre = bmdentrynbre;
            this.devdecimalordinal = devdecimalordinal;
            this.typverifcomptemidasordinal = typverifcomptemidasordinal;
        }

        public string Cendid
        {
            get { return cendid; }
            set { cendid = value; }
        }

        public string Bmhfmc
        {
            get { return bmhfmc; }
            set { bmhfmc = value; }
        }

        public double Bmhdonemidasstep1
        {
            get { return bmhdonemidasstep1; }
            set { bmhdonemidasstep1 = value; }
        }

        public double Bmhdonemidasstep2
        {
            get { return bmhdonemidasstep2; }
            set { bmhdonemidasstep2 = value; }
        }

        public double Lmiid
        {
            get { return lmiid; }
            set { lmiid = value; }
        }

        public double Bmhhold
        {
            get { return bmhhold; }
            set { bmhhold = value; }
        }

        public double Bmhbatchmidas
        {
            get { return bmhbatchmidas; }
            set { bmhbatchmidas = value; }
        }

        public DateTime Headervaluedateordinal
        {
            get { return headervaluedateordinal; }
            set { headervaluedateordinal = value; }
        }

        public double Sunid
        {
            get { return sunid; }
            set { sunid = value; }
        }

        public string Bradid
        {
            get { return bradid; }
            set { bradid = value; }
        }

        public double Bmhid
        {
            get { return bmhid; }
            set { bmhid = value; }
        }

        public string Bmdrubirique
        {
            get { return bmdrubirique; }
            set { bmdrubirique = value; }
        }

        public string Bmdclient
        {
            get { return bmdclient; }
            set { bmdclient = value; }
        }

        public string Bmdsequence
        {
            get { return bmdsequence; }
            set { bmdsequence = value; }
        }

        public DateTime Bmddate
        {
            get { return bmddate; }
            set { bmddate = value; }
        }

        public string Bmddevise
        {
            get { return bmddevise; }
            set { bmddevise = value; }
        }

        public double Bmdmontant
        {
            get { return bmdmontant; }
            set { bmdmontant = value; }
        }

        public string Bmdtype
        {
            get { return bmdtype; }
            set { bmdtype = value; }
        }

        public string Bmdnote
        {
            get { return bmdnote; }
            set { bmdnote = value; }
        }

        public string Bmdentrynbre
        {
            get { return bmdentrynbre; }
            set { bmdentrynbre = value; }
        }

        public double Devdecimalordinal
        {
            get { return devdecimalordinal; }
            set { devdecimalordinal = value; }
        }

        public double Typverifcomptemidasordinal
        {
            get { return typverifcomptemidasordinal; }
            set { typverifcomptemidasordinal = value; }
        }

现在,当我使用 dapper 将查询执行到列表中时

Connection conn = new Connection();
 OracleConnection connection = conn.GetDBConnection();
  myList= connection.Query<Batch>(querySql).ToList();

现在,在调试时,所有字段 returns 都是预期值。但是,我注意到下面的字段 null in myList 不是空的,但实际上是 null ,但问题是它们在数据库中不为空

Bmdrubirique , Sunid, Bmdentrynbre, Bradid ,Cenid

在 oracle 数据库中,这些字段如下所示:

CENID is VARCHAR2(3 BYTE)` 
Bmhid is VARCHAR2(3 BYTE) 
Sunid is NUMBER(38,0)
Bradid is VARCHAR2(3 BYTE)

我不明白,哪里出错了?为什么其他字段在 returns null 值时正确加载?

我的默认假设是实际代码中存在拼写错误,并且构造函数正在将字段中的值分配给自身。但是,坦率地说:由于您有一个 public Batch() {} 构造函数,我不确定第二个构造函数的好处是什么 - 它只会增加出错的风险。字段和手动属性也是如此。

因此,如果像我一样,您当前拥有的位置(简化为两个属性):

public class Batch
{
    private string cendid;
    private string bmhfmc;
    public Batch() {}
    public Batch(string cendid, string bmhfmc)
    {
        this.cendid = cendid;
        this.bmhfmc = bmhfmc;
    }

    public string Cendid
    {
        get { return cendid; }
        set { cendid = value; }
    }

    public string Bmhfmc
    {
        get { return bmhfmc; }
        set { bmhfmc = value; }
    }
}

我只会:

public class Batch
{
    public string Cendid {get;set;}
    public string Bmhfmc {get;set;}
}

所有其余代码只是出现编码错误的机会。

现在:Cendidnull 的原因是:该列是 CENID - 只有一个 d。这意味着 dapper 甚至没有使用您的自定义构造函数,因为它不是构造函数和列之间的完美匹配。同上其他字段,例如 BRAID vs BRADID.

所以接下来要做的就是修正拼写错误。