Dapper failing to read SQL uniqueidentifier type to string

2

I have the following class:

public class User
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string DisplayName { get; set; }
        public DateTime RegistrationDateTime { get; set; }
        public DateTime LastLoginDateTime { get; set; }
        public bool IsAdmin { get; set; }

        public IEnumerable<Session> Sessions { get; set; }

        public class Session
        {
            public string Id { get; set; }
            public string UserId { get; set; }
            public User User { get; set; }
            public DateTime StartDateTime { get; set; }
            public DateTime EndDateTime { get; set; }
        }
    }

In my database, the Id is set to a uniqueidentifier datatype.

When I execute the following with Dapper implemented, the error message below appears:

public User.Session UpsertSession(User.Session session)
        {
            var item = SqlConnection.QuerySingle<User.Session>("[dbo].[UserUpsertSession]",
                new { session.UserId }, commandType: CommandType.StoredProcedure);
            return item;
        }

Error:

System.Data.DataException
  HResult=0x80131501
  Message=Error parsing column 0 (Id=689bfce8-333c-431e-8a35-60f85153d0c5 - Object)
  Source=Dapper
  StackTrace:
   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 3609
   at Dapper.SqlMapper.QueryRowImpl[T](IDbConnection cnn, Row row, CommandDefinition& command, Type effectiveType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1199
   at Dapper.SqlMapper.QuerySingle[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 783
   at Database.Managers.UserManager.UpsertSession(Session session) in C:\VSTS\Strategic Development\Applications\NotificationSystem\Main\Data\Managers\UserManager.cs:line 63
   at NotificationSystem.Web.Infrastructure.UsageTrackingActionFilter.OnActionExecuted(ActionExecutedContext context) in C:\VSTS\Strategic Development\Applications\NotificationSystem\Main\Web\Infrastructure\UsageTrackingActionFilter.cs:line 20
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()

Inner Exception 1:
InvalidCastException: Object must implement IConvertible.

This seems to be some type of mapping issue between types. I imagine others have encountered this as well but I haven't been able to find a solution to this anywhere.

c#
sql-server
dapper
asked on Stack Overflow Feb 26, 2019 by Adam Chubbuck

1 Answer

3

It seems the key is to use Guid? in your entity class, as shown below.

public class User
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string DisplayName { get; set; }
        public DateTime RegistrationDateTime { get; set; }
        public DateTime LastLoginDateTime { get; set; }
        public bool IsAdmin { get; set; }

        public IEnumerable<Session> Sessions { get; set; }

        public class Session
        {
            //public string Id { get; set; }
            public Guid? Id { get; set; }
            public string UserId { get; set; }
            public User User { get; set; }
            public DateTime StartDateTime { get; set; }
            public DateTime EndDateTime { get; set; }
        }
    }

When sending the value to the service, you must convert it to a string before it is sent:

public User.Session UpsertSession(string userId, Guid? sessionId = null)
        {
            var item = SqlConnection.QuerySingle<User.Session>("[dbo].[UserUpsertSession]",
                new { userId, sessionId = sessionId?.ToString() }, commandType: CommandType.StoredProcedure);
            return item;
        }
answered on Stack Overflow Feb 26, 2019 by Adam Chubbuck

User contributions licensed under CC BY-SA 3.0