Skip to content

Commit b7d1873

Browse files
committed
fix guid casting
1 parent 6601449 commit b7d1873

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/TableStorage.Abstractions.TableEntityConverters/EntityConvert.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,19 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
4646
Expression<Func<T, object>> partitionProperty,
4747
Expression<Func<T, object>> rowProperty) where T : new()
4848
{
49-
return FromTableEntity(entity, partitionProperty, p => Convert.ChangeType(entity.PartitionKey, typeof(TP)),
50-
rowProperty, p => Convert.ChangeType(entity.RowKey, typeof(TR)));
49+
var convertPartition = new Func<string,TP>(p=>(TP)Convert.ChangeType(p, typeof(TP)));
50+
var convertRow = new Func<string,TR>(r =>(TR)Convert.ChangeType(r, typeof(TR)));
51+
52+
if (typeof(TP) == typeof(Guid))
53+
{
54+
convertPartition = p=>(TP)(object)Guid.Parse(p);
55+
}
56+
if (typeof(TR) == typeof(Guid))
57+
{
58+
convertRow = r => (TR)(object)Guid.Parse(r);
59+
}
60+
return FromTableEntity(entity, partitionProperty, convertPartition,
61+
rowProperty, convertRow);
5162
}
5263

5364
public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,26 @@ public void convert_from_entity_table_unmapped_partition_key()
118118

119119
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
120120
}
121+
122+
public class GuidKeyTest
123+
{
124+
public Guid A { get; set; }
125+
public Guid B { get; set; }
126+
}
127+
128+
[Fact]
129+
public void convert_from_entity_table_with_guid_keys()
130+
{
131+
Guid a = Guid.Parse("7ba5bd25-823e-4c01-940e-1f131cbed8ed");
132+
Guid b = Guid.Parse("603e51de-950e-4270-a755-c26950742103");
133+
134+
var obj = new GuidKeyTest{A = a, B = b};
135+
var e = obj.ToTableEntity(x => x.A, x => x.B);
136+
var convertedObject = e.FromTableEntity<GuidKeyTest, Guid, Guid>(x => x.A, x => x.B);
137+
138+
139+
Assert.Equal(a, convertedObject.A);
140+
141+
}
121142
}
122143
}

0 commit comments

Comments
 (0)