Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit eb3eb80

Browse files
committed
Move conversion to Enum Type into EnumConverter
1 parent f33474d commit eb3eb80

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

src/ServiceStack.OrmLite.MySql.Tests/EnumTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ public void CanQueryByNullableEnumValue_using_where_with_AnonType()
135135
[Test]
136136
public void CanSaveNullableEnum_with_specific_id_select_with_anon_type()
137137
{
138-
using (var con = OpenDbConnection())
138+
using (var db = OpenDbConnection())
139139
{
140-
con.CreateTableIfNotExists<MyObj>();
140+
db.DropAndCreateTable<MyObj>();
141141
var myObj = new MyObj();
142142
myObj.Id = 1;
143143
myObj.Test = MyEnum.One;
144-
con.Insert(myObj);
144+
db.Insert(myObj);
145145

146-
myObj = con.Single<MyObj>(new {Id = 1});
146+
myObj = db.Single<MyObj>(new {Id = 1});
147147

148148
Assert.That(myObj.Id, Is.EqualTo(1));
149149
Assert.That(myObj.Test, Is.Not.EqualTo(null));
@@ -154,13 +154,13 @@ public void CanSaveNullableEnum_with_specific_id_select_with_anon_type()
154154
[Test]
155155
public void CanSaveNullableEnum_with_specific_id_select_with_type()
156156
{
157-
using (var existsCon = OpenDbConnection())
157+
using (var db = OpenDbConnection())
158158
{
159-
existsCon.CreateTableIfNotExists<MyObj>();
160-
var exists = existsCon.SingleById<MyObj>(1);
159+
db.DropAndCreateTable<MyObj>();
160+
var exists = db.SingleById<MyObj>(1);
161161
if (exists != null)
162162
{
163-
existsCon.DeleteById<MyObj>(1);
163+
db.DeleteById<MyObj>(1);
164164
}
165165
}
166166

src/ServiceStack.OrmLite/Converters/SpecialConverters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override object FromDbValue(Type fieldType, object value)
5959
if (strVal != null)
6060
return Enum.Parse(fieldType, strVal, ignoreCase:true);
6161

62-
return Convert.ChangeType(value, fieldType.GetTypeCode());
62+
return Enum.ToObject(fieldType, value);
6363
}
6464
}
6565

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,7 @@ public static T PopulateWithSqlReader<T>(this T objWithProperties,
341341
else
342342
{
343343
var fieldValue = converter.FromDbValue(fieldDef.FieldType, value);
344-
if (fieldDef.FieldType.IsEnum && fieldDef.IsNullable)
345-
{
346-
var enumType = Nullable.GetUnderlyingType(fieldDef.PropertyInfo.PropertyType);
347-
var enumValue = Enum.ToObject(enumType, value);
348-
fieldDef.PropertyInfo.SetProperty(objWithProperties, enumValue);
349-
}
350-
else
351-
{
352-
fieldDef.SetValueFn(objWithProperties, fieldValue);
353-
}
344+
fieldDef.SetValueFn(objWithProperties, fieldValue);
354345
}
355346
}
356347
}

0 commit comments

Comments
 (0)