igor-tkachev/bltoolkit — pull request #232 by darK-Zi0n-te4am-cr3vv (2013-05-17 00:43:45+00:00)
Title: Different enum mapping behavior in ExecuteObject and ExecuteScalar

I've noticed the difference in behaviour when value from DB gets mapped to the enum variable.

When you're trying to map value to enum property inside some coplex type (e.g. Gender in Person) using ```ExecuteObject()``` or ```ExecuteList()``` methods of ```DbManager```, values like 'M' gets mapped correctly. But when you're trying to use ```ExecuteScalar()``` for scalar, values like 'M' causes failure.

Test cases below shows this difference in mapping behavior.

This code works as expected:
```cs
[Test]
public void EnumInClassTest()
{
using (var dbm = new DbManager())
{
const sting command =
"select 1 as [PersonID], 'A' as FirstName, " +
"'B' as MiddleName, 'C' as LastName, 'M' as Gender";

var person = dbm.SetCommand(CommandType.Text, command)
.ExecuteObject();

Assert.AreEqual(1, person.ID);
Assert.AreEqual("A", person.FirstName);
Assert.AreEqual("B", person.MiddleName);
Assert.AreEqual("C", person.LastName);
Assert.AreEqual(Gender.Male, person.Gender);
}
}
```

And this fails with ```System::FormatException```:
```cs
[Test]
public void ExecuteScalarMappedEnumTest()
{
using (var dbm = new DbManager())
{
var gender = dbm.SetCommand(CommandType.Text, "select 'M'")
.ExecuteScalar();

Assert.That(gender, Is.EqualTo(Gender.Male));
}
}
```

In the second case, BlToolkit tries to map 'M' as integer, which causes failure.

I suggesting to change this behavior by applying this PR.

As I can understand, this change will not break current behavior. This test shows, that maping integers to enums in ```ExecuteScalar()``` still working as it was:
```cs
[Test]
public void EnumExecuteScalarIntegerTest()
{
using (var dbm = new DbManager())
{
var gender = dbm.SetCommand(CommandType.Text, "select 1")
.ExecuteScalar();

Assert.That(gender, Is.EqualTo(Gender.Male));
}
}
```

I've runned all tests in project, but I have only SQL Server 2008 as test environment. 8 tests of Data::Linq::ConvertTests (ToChar, ToDefaultChar, ToDefaultNChar, ToDefaultNVarChar, ToDefaultVarChar, ToNChar, ToNVarChar, ToVarChar) fails in my environment, but it also fails without my changes. Anyway, these tests seems completely unrelated to this change for me.

So, if this change not breaks existing tests an behavior, it would be great to see it in BlToolkit mainline since we have some code dependent on ```ExecuteScalar()``` for enums.