I am facing a problem using Dapper.
I have two models:
public class ClientEventsModel
{
public int Id { get; set; }
public int ClientId { get; set; }
public ClientEventTypeLog EventType {get; set;}
public int Value { get; set; }
public DateTime Date { get; set; }
public string? Doer { get; set; }
}
[Serializable]
public class ExtentedClientEventsModel : ClientEventsModel
{
public List<string> Values { get; set; } = new List<string>();
}
One is inherited from the other.
And a request in the form:
var sqlStr = <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a185e1">[email protected]</a>"SELECT ce.Id, ce.ClientId, ce.EventType, ce.Value, ce.Date, a.UserName AS Doer, cevn.`Values` FROM clients.client_events ce
LEFT JOIN `z-price`.aspnetusers a ON ce.Doer = a.Id_num
LEFT JOIN clients.clients_events_value_name cevn ON ce.Id = cevn.ClientEventsId
where ClientId = {clientId} and Date BETWEEN '{from.ToMysql()}' and '{to.AddDays(1).ToMysql()}'";
var result = DefaultConnection.Query<ExtentedClientEventsModel>(sqlStr);
When I execute the query in the client it returns 16 records. But when I use Dapper it returns 4 records. And only those with the Doer == null field.
I tried replacing the model with a dynamic type, but it didn’t help.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Dapper doesn’t get to invent the number of rows coming back. If Dapper says there were 4 rows, then one of two things:
- you’ve found a niche edge case that hasn’t been seen in the many years of Dapper
- your query really does only return 4 rows
Now, I’m open to “1”, but in reality “2” is much more likely. I suspect the real problem here is the parameters – or lack there-of. Never ever use concatenation to build SQL from values. Try instead:
const string sqlStr = @"
SELECT ce.Id, ce.ClientId, ce.EventType, ce.Value, ce.Date, a.UserName AS Doer, cevn.`Values` FROM clients.client_events ce
LEFT JOIN `z-price`.aspnetusers a ON ce.Doer = a.Id_num
LEFT JOIN clients.clients_events_value_name cevn ON ce.Id = cevn.ClientEventsId
where ClientId = @clientId and Date BETWEEN @from and @to";
var result = DefaultConnection.Query<ExtentedClientEventsModel>(
sqlStr, new { clientId, from, to = to.AddDays(1) }).AsList();
(note: different databases have different syntax for parameters; if @clientId etc doesn’t work, try :clientId, $clientId, etc)
Method 2
This is what the request gives in the DbForge client

And this is what Dapper returns:

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
