Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

dapper

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.

like image 552
Stephan Møller Avatar asked Jan 21 '13 12:01

Stephan Møller


1 Answers

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.

like image 89
bkaid Avatar answered Oct 23 '22 13:10

bkaid