Search code examples
c#sqldapper

List of bools always returns false


When trying to get a list of bools to map to my properties, the list always return false for all items eventhough some of them are true. In the SQL db those columns are setup as bit, in my code these are booleans.

When I run my query directly in SQL I get the desired results, but debugging the same query in VS and looking at the output, the returned values stay false. I've manually ran an update query in the db to ensure some values are true, but still they return false. In the method I already tried to clear the list before filling it up but doesn't help.

DataAccess using Dapper:

    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 class:

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; }
}

In the 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;
        }
    }

In the database, OnBPInvoice (Invoice_Promo column) and OnBPCopyInvoice (CopyInvoice column) are true. I want to get the correct results back.


Solution

  • Dapper does provide out of the box mapping like EF does(with [Column("MyColumnName")]

    Meaning that the class properties must be exactly as they are written in the DB

    Meaning that Invoice_Promo (as in your SELECT statement) must be named

    public bool Invoice_Promo{ get; set; }

    If you want to can create your custom mapper and use it like so

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

    and then initialized you mapper

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

    More info you can find here