我在 .NET 4.5 中使用 dapper 来轻松查询我的 MS SQl 数据库。以下工作正常:
没问题:
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();
选择“Id”列的问题 - 引发 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();
抛出异常并显示消息“操作可能会破坏运行时”。
谁能指出我在这里可能做错了什么?我的GroupWithRecipientCount类如下所示:
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; }
}
编辑1:
我删除了这个:
如果我将 Id 列重命名为“Id”以外的其他名称,则一切正常,即
"SELECT g.Id SomeOtherName, g.Name, ..."
如果将我的班级的 Id 字段重命名为 SomeOtherName,看起来它也会中断,所以我猜这个问题与映射 Id 字段有关,但这只是一个猜测。