Dapper and "Operation could destabilize the runtime" when naming column 'Id'

.net dapper

Question

I use dapper in .NET 4.5 to ease querying my MS SQl database. The following works just fine:

NO PROBLEMS:

const string sql = @"
SELECT g.Name, g.Slug, g.CreatedDate, COUNT(r.Id) recipientCount
FROM Groups g
LEFT JOIN GroupRecipients r ON r.GroupId = g.Id
WHERE g.CustomerId = @CustomerId
GROUP BY g.Id, g.Name, g.Slug, g.CreatedDate

";

return _connection.Query(sql, new { CustomerId = customerId }).ToList();

PROBLEM SELECTING 'Id' COLUMN - RAISES VerificationException:

const string sql = @"
SELECT g.Id, g.Name, g.Slug, g.CreatedDate, COUNT(r.Id) recipientCount
FROM Groups g
LEFT JOIN GroupRecipients r ON r.GroupId = g.Id
WHERE g.CustomerId = @CustomerId
GROUP BY g.Id, g.Name, g.Slug, g.CreatedDate

";
return _connection.Query<GroupWithRecipientCount>(sql, new { CustomerId = customerId }).ToList();

The exception is thrown with the message 'Operation could destabilize the runtime.'

Can anyone pinpoint what I may be doing wrong here? My GroupWithRecipientCount class looks like this:

public class GroupWithRecipientCount
{
    public int Id { get; set; }
    public DateTime CreatedDate { get; set; }
    public string Name { get; private set; }
    public string Slug { get; private set; }
    public int RecipientCount { get; private set; }
}

EDIT 1:

I removed this:

If I rename the Id column to something else than 'Id', everything works fine, i.e.

"SELECT g.Id SomeOtherName, g.Name, ..."

Looks like it also breaks if rename my class' Id field to SomeOtherName, so I guess the problem is related to mapping the Id field, but thats just a guess.

Accepted Answer

I got this exception when my model class had the wrong data type when mapping to a SQL field. My model was an (wrongly) an int and the SQL statement was returning a string. Double-check your data types if you get this cryptic error message.


Popular Answer

It turned out some security issue I had choked the original exception and threw another exception instead.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why