diff --git a/Mapper.UnitTests/CommandExtensionTests.cs b/Mapper.UnitTests/CommandExtensionTests.cs index 7604283..cfe487e 100644 --- a/Mapper.UnitTests/CommandExtensionTests.cs +++ b/Mapper.UnitTests/CommandExtensionTests.cs @@ -165,6 +165,17 @@ public void can_add_null_DateTime() Assert.AreEqual(DBNull.Value, cmd.Parameters[0].Value); } + [Test] + public void can_add_enum_as_int() + { + SqlCommand cmd = new SqlCommand(); + cmd.AddParameters(new { SomeId = TestEnum.Something }); + Assert.AreEqual(1, cmd.Parameters.Count); + Assert.AreEqual("@SomeId", cmd.Parameters[0].ParameterName); + Assert.AreEqual(DbType.Int32, cmd.Parameters[0].DbType); + Assert.AreEqual(1, cmd.Parameters[0].Value); + } + [Test] public void can_add_null_TableType() { diff --git a/Mapper/CommandExtension.cs b/Mapper/CommandExtension.cs index 6e7b174..4029e95 100644 --- a/Mapper/CommandExtension.cs +++ b/Mapper/CommandExtension.cs @@ -155,13 +155,14 @@ private static Action CreateAddParametersAction(Type type) var createParameter = typeof(IDbCommand).GetMethod("CreateParameter", Type.EmptyTypes); foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { - if (!Types.IsStructured(prop.PropertyType) && !Types.TypeToDbType.ContainsKey(prop.PropertyType)) continue; + var propertyType = prop.PropertyType; + if (!IsSupportedType(propertyType)) continue; lines.Add(Expression.Assign(dataParam, Expression.Call(cmd, createParameter))); lines.Add(Expression.Assign(Expression.Property(dataParam, "ParameterName"), Expression.Constant("@" + prop.Name))); - if (Types.IsStructured(prop.PropertyType)) + if (Types.IsStructured(propertyType)) { - if (prop.PropertyType != typeof(TableType)) + if (propertyType != typeof(TableType)) throw new NotSupportedException($"Parameter {dataParam.Name} implements {nameof(IEnumerable)} but type name is unknown. Please wrap parameter by calling {nameof(SqlDataRecordExtensions.WithTypeName)}"); lines.Add(Expression.IfThen( @@ -171,12 +172,16 @@ private static Action CreateAddParametersAction(Type type) lines.Add(Expression.Assign(Expression.Property(Expression.Convert(dataParam, typeof (SqlParameter)), "SqlDbType"), Expression.Constant(SqlDbType.Structured))); lines.Add(Expression.Assign(Expression.Property(Expression.Convert(dataParam, typeof (SqlParameter)), "TypeName"), Expression.Property(Expression.Property(parameters, prop.Name), "TypeName"))); } + else if (propertyType.IsEnum) + { + lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(DbType.Int32))); + } else { - lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(Types.TypeToDbType[prop.PropertyType]))); + lines.Add(Expression.Assign(Expression.Property(dataParam, "DbType"), Expression.Constant(Types.TypeToDbType[propertyType]))); } - if (Types.CanBeNull(prop.PropertyType)) + if (Types.CanBeNull(propertyType)) { lines.Add(Expression.IfThenElse( Expression.Equal(Expression.Property(parameters, prop.Name), Expression.Constant(null)), @@ -195,9 +200,19 @@ private static Action CreateAddParametersAction(Type type) return Expression.Lambda>(block, cmd, obj).Compile(); } + private static bool IsSupportedType(Type propertyType) + { + return Types.IsStructured(propertyType) || Types.TypeToDbType.ContainsKey(propertyType) || propertyType.IsEnum; + } + private static UnaryExpression PropertyValue(ParameterExpression obj, PropertyInfo prop) { - return Expression.Convert(Expression.Property(obj, prop.Name), typeof(object)); + Expression value = Expression.Property(obj, prop.Name); + if (prop.PropertyType.IsEnum) + { + value = Expression.Convert(value, typeof(int)); + } + return Expression.Convert(value, typeof(object)); } } diff --git a/Mapper/Mapper.nuspec b/Mapper/Mapper.nuspec index 6e31097..f1076c2 100644 --- a/Mapper/Mapper.nuspec +++ b/Mapper/Mapper.nuspec @@ -2,7 +2,7 @@ Mapper - 1.0.1.1 + 1.0.1.2 BusterWood A convention-based object cloner, object-object mapper (like AutoMapper), IDataReader to object mapper, object to IDbDataParameter mapper, etc. https://github.com/busterwood/mapper diff --git a/Mapper/Properties/AssemblyInfo.cs b/Mapper/Properties/AssemblyInfo.cs index b327563..9f35ff2 100644 --- a/Mapper/Properties/AssemblyInfo.cs +++ b/Mapper/Properties/AssemblyInfo.cs @@ -12,6 +12,6 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.1.1")] +[assembly: AssemblyFileVersion("1.0.1.2")] [assembly: InternalsVisibleTo("Mapper.UnitTests")] \ No newline at end of file