布尔列表始终返回false

时间:2019-07-07 06:18:26

标签: c# sql dapper

当尝试获取布尔变量列表以映射到我的属性时,即使其中的某些项为true,该列表对于所有项始终返回false。在SQL db中,这些列被设置为位,在我的代码中,这些列是booleans

当我直接在SQL中运行查询时,可以获得所需的结果,但是在VS中调试相同的查询并查看输出时,返回的值保持为false。我已经在db中手动运行了一个更新查询,以确保某些值是true,但是它们仍然返回false。在该方法中,我已经尝试在填充列表之前清除列表,但无济于事。

使用Dapper的DataAccess:

    public List<PromoLinesBPConditions> GetBlankPaperConditions()
    {
    using (IDbConnection connection = new 
    System.Data.SqlClient.SqlConnection(Helper.CnnVal(_dbNameEng)))
    {
    var output = connection.Query<PromoLinesBPConditions>($"SELECT 
    Invoice_Promo, CopyInvoice_Promo, Folio_Promo, ConfLetter_Promo, 
    BQTOffer_Promo, BQTPO_Promo, Reminder_Promo, ProForma_Promo, 
    Message_Promo from RPTSET_Invoice_Parameters").ToList();
    return output;
    }
    }

PromoLinesBPConditions类:

public class PromoLinesBPConditions
{
    public bool OnBPInvoice { get; set; } 
    public bool OnBPCopyInvoice { get; set; }
    public bool OnBPFolio { get; set; }
    public bool OnBPConfLetter { get; set; }
    public bool OnBPBQTOffer { get; set; }
    public bool OnBPBQTPO { get; set; }
    public bool OnBPReminder { get; set; }
    public bool OnBPProForma { get; set; }
    public bool OnBPMessage { get; set; }
}

在DetailViewModel中:

    private void GetBlankPaperConditions()
    {
        BlankPaperConditions = _dataService.GetBlankPaperConditions();
        foreach (var item in BlankPaperConditions)
        {
            OnBPInvoice = item.OnBPInvoice;
            OnBPCopyInvoice = item.OnBPCopyInvoice;
            OnBPFolio = item.OnBPFolio;
            OnBPConfLetter = item.OnBPConfLetter;
            OnBPBQTOffer = item.OnBPBQTOffer;
            OnBPBQTPO = item.OnBPBQTPO;
            OnBPReminder = item.OnBPReminder;
            OnBPProForma = item.OnBPProForma;
            OnBPMessage = item.OnBPMessage;
        }
    }

在数据库中,OnBPInvoice (Invoice_Promo column)OnBPCopyInvoice (CopyInvoice column)为true。我想获得正确的结果。

1 个答案:

答案 0 :(得分:3)

Dapper确实像EF一样提供了开箱即用的映射(带有[Column(“ MyColumnName”)]

这意味着类属性必须精确地写在数据库中

表示 Invoice_Promo (如您的 SELECT 语句中所示)必须命名为

public bool Invoice_Promo{ get; set; }

如果您想创建自定义映射器并像这样使用它

     public class MyMapper : EntityMap<MyClassName>
     {
         public MyMapper()
         {
             Map(i => i.MyPropery).ToColumn("MyCustomPropery");
         }   

     }

然后初始化您的映射器

FluentMapper.Initialize(config =>
{
    config.AddMap(new MyMapper());
});

更多信息,您可以找到here