Skip to content

Commit

Permalink
Fixes #65 by updating InferDbType to return DbType.String for enums. …
Browse files Browse the repository at this point in the history
…This will allow them to be passed via the dynamic syntax, or in a TVP without doing anything special.
  • Loading branch information
abe545 committed Feb 20, 2016
1 parent 2a86e44 commit 9774474
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ static HierarchicalTypeRowFactory()
types.Enqueue(childType);

var foreignKeyName = t.Name + "Id";
var fkAttr = (ForeignKeyAttribute)Attribute.GetCustomAttribute(child, typeof(ForeignKeyAttribute));
var fkAttr = child.GetCustomAttributes(typeof(ForeignKeyAttribute), true).OfType<ForeignKeyAttribute>().FirstOrDefault();
if (fkAttr != null)
foreignKeyName = fkAttr.Name;
else if (interfaceProperties != null)
{
var interfaceProp = interfaceProperties.FirstOrDefault(p => p.Name == child.Name);
fkAttr = (ForeignKeyAttribute)Attribute.GetCustomAttribute(interfaceProp, typeof(ForeignKeyAttribute));
fkAttr = interfaceProp.GetCustomAttributes(typeof(ForeignKeyAttribute), true).OfType<ForeignKeyAttribute>().FirstOrDefault();
if (fkAttr != null)
foreignKeyName = fkAttr.Name;
}
Expand Down Expand Up @@ -148,13 +148,13 @@ private static PropertyInfo GetKeyProperty(string className, IEnumerable<Propert
Contract.Requires(!string.IsNullOrWhiteSpace(className));
Contract.Requires(props != null && Contract.ForAll(props, p => p != null));

var explicitKey = props.Where(p => p.CanRead && Attribute.GetCustomAttribute(p, typeof(KeyAttribute)) != null).SingleOrDefault();
var explicitKey = props.Where(p => p.CanRead && p.GetCustomAttributes(typeof(KeyAttribute), true).Any()).SingleOrDefault();
if (explicitKey != null)
return explicitKey;

if (interfaceProperties != null)
{
explicitKey = interfaceProperties.Where(p => p.CanRead && Attribute.GetCustomAttribute(p, typeof(KeyAttribute)) != null).SingleOrDefault();
explicitKey = interfaceProperties.Where(p => p.CanRead && p.GetCustomAttributes(typeof(KeyAttribute), true).Any()).SingleOrDefault();
if (explicitKey != null)
return explicitKey;
}
Expand Down
2 changes: 1 addition & 1 deletion CodeOnlyStoredProcedure/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal static DbType InferDbType(this Type type)
return DbType.Decimal;
else if (type == typeof(Boolean))
return DbType.Boolean;
else if (type == typeof(String))
else if (type == typeof(String) || type.IsEnum)
return DbType.String;
else if (type == typeof(DateTime))
return DbType.DateTime;
Expand Down
15 changes: 15 additions & 0 deletions CodeOnlyTests/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using System.Collections.Generic;
using System.Data;

#if NET40
namespace CodeOnlyTests.Net40
Expand Down Expand Up @@ -422,6 +423,15 @@ public void ReturnsATableValuedParameterForValidType()
}
#endregion

#region InferDbType
[TestMethod]
public void InferDbType_ReturnsString_ForEnum()
{
var result = typeof(EnumToTest).InferDbType();
result.Should().Be(DbType.String, "because an enum should be passed as a string");
}
#endregion

#region Types To Test With
private interface IModel
{
Expand Down Expand Up @@ -483,6 +493,11 @@ private class WithOptionalProperty
public string Foo { get; set; }
public string Bar { get; set; }
}

private enum EnumToTest
{
One, Two
}
#endregion
}
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.2.5.{build}
skip_tags: false

# Operating system (build VM template)
os: Visual Studio 2015
os: Visual Studio 2013

branches:
# blacklist gh-pages, since the documentation branch shouldn't be built
Expand Down

0 comments on commit 9774474

Please sign in to comment.