I'm switching from ActiveRecord to Dapper. I have a simple Project class with a couple of fields:
- Id (int)
- Name (string)
- Secret (guid)
- OwnerId (guid)
My initial attempt to use Dapper resulted in this code: I'm using Dapper like so:
List<Project> projects = new List<Project>();
using (var conn = MvcApplication.GetSqlConnection()) // new connection
{
projects = conn.Query<Project>("SELECT * FROM Projects p WHERE p.OwnerId = (SELECT UserId FROM aspnet_Users WHERE LoweredUserName=@userName)", new { userName = User.Identity.Name.ToLower() }).ToList();
}
return View(projects);
This query resulted in a DataException: Error parsing column 0 (Id=11 - Int16)
. The inner exception is an InvalidCastException: Specified cast is not valid.
Since none of the samples on the Dapper usage page mention SELECT *
, I tried to reform my query like so:
projects = conn.Query<Project>("SELECT OwnerId = (SELECT UserId FROM aspnet_Users WHERE LoweredUserName=@userName)", new { userName = User.Identity.Name.ToLower() }).ToList();
When I debug, although I have two projects in my DB, I only get one back; and that one has null for a name.
Strange. What am I doing wrong? I feel like Dapper is not mapping the fields (DB) to the fields (class) properly. I'm using the latest NuGet version.