diff --git a/Dapper/SqlMapper.cs b/Dapper/SqlMapper.cs index 563280d2013af211362f7e9ae8e2ac8976d6de59..5c3b10bc187d1fbe0d5337ebf1f814fe00b05d00 100644 --- a/Dapper/SqlMapper.cs +++ b/Dapper/SqlMapper.cs @@ -187,6 +187,12 @@ static SqlMapper() private static DbType LookupDbType(Type type) { DbType dbType; + var nullUnderlyingType = Nullable.GetUnderlyingType(type); + if (nullUnderlyingType != null) type = nullUnderlyingType; + if (type.IsEnum) + { + type = Enum.GetUnderlyingType(type); + } if (typeMap.TryGetValue(type.TypeHandle, out dbType)) { return dbType; @@ -197,6 +203,7 @@ private static DbType LookupDbType(Type type) return DbType.Xml; } + throw new NotSupportedException(string.Format("The type : {0} is not supported by dapper", type)); } diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 25e42f2559f8d218b1f87064d9cf096366315280..8af9cd0ec5bc3c409b374114253c46e6ef11efdc 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -97,6 +97,27 @@ public void TestStrings() .IsSequenceEqualTo(new[] { "a", "b" }); } + enum EnumParam : short + { + None, A, B + } + class EnumParamObject + { + public EnumParam A { get; set; } + public EnumParam? B { get; set; } + public EnumParam? C { get; set; } + } + public void TestEnumParams() + { + EnumParam a = EnumParam.A; + EnumParam? b = EnumParam.B, c = null; + var obj = connection.Query("select @a as A, @b as B, @c as C", + new { a, b, c }).Single(); + obj.A.IsEqualTo(EnumParam.A); + obj.B.IsEqualTo(EnumParam.B); + obj.C.IsEqualTo(null); + } + public class Dog { public int? Age { get; set; }