dapper nuget 1.7枚举映射

Ant*_*san 30 c# mysql enums dapper

我从Nuget(v 1.7)升级到最新版本的Dapper后遇到了一个问题.

它总是返回第一个枚举成员(也就是说,它无法映射).

我使用MySQL作为数据库.

CREATE TABLE `users_roles` (
    `userId` INT(11) NOT NULL,
    `roleId` INT(11) NOT NULL,  
    KEY `user_id` (`userId`),
    KEY `role_id` (`roleId`)
);

INSERT INTO `users_roles` (`userId`, `roleId`) VALUES (1, 1);
INSERT INTO `users_roles` (`userId`, `roleId`) VALUES (2, 2);

public enum Role { 
  Anonymous = 0, Authenticate = 1, Administrator = 2
}

var role = Current.Db.Query<Role>(@"SELECT roleId as Role FROM users_roles
    WHERE userId=@id", new { id = 2 }).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

它给出了Dapper nuget v1.6中的预期输出.这是新版本(1.7)的正确行为吗?

更新:

在使用一些控制台应用程序和新的mvc3应用程序进行一些测试后,我发现当您直接映射枚举类型时,Dapper枚举映射的行为是不一致的.

但是,将枚举作为类的属性进行映射会以某种方式始终返回正确的映射

public class User
{
   public int Id { get; set; }
   public Role Role { get; set; }
}

var user = Current.Db.Query<User>(@"SELECT roleId as Role, userId as Id 
    FROM users_roles
    WHERE userId=@id", new { id = 2 }).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

user.Role的结果以某种方式返回预期的输出

Sla*_*aGu 1

在错误修复之前,我的解决方法是使用额外条件修改 GetDeserializer 方法

|| 类型.IsEnum

使用 struct deserializer 进行枚举,如下所示:

        private static Func<IDataReader, object> GetDeserializer(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing)
        {
...
            if (!(typeMap.ContainsKey(type) || type.IsEnum /* workaround! */ || type.FullName == LinqBinary))
            {
                return GetTypeDeserializer(type, reader, startBound, length, returnNullIfFirstMissing);
            }
            return GetStructDeserializer(type, startBound);

        }
Run Code Online (Sandbox Code Playgroud)