1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Collections . ObjectModel ;
34using System . ComponentModel . DataAnnotations ;
45using System . Linq ;
@@ -96,7 +97,7 @@ INNER JOIN (VALUES (7, N'black_de', N'black')
9697 } ) ;
9798
9899 [ TestMethod ]
99- public Task QuerySingle_WithAutoMultiMap_NonEntity_Collection_ReferenceEqualityIsPerformed ( ) => base . ExecuteTest ( accessor =>
100+ public Task QuerySingle_WithAutoMultiMap_NonEntityNonPrimitive_Collection_ReferenceEqualityIsPerformed ( ) => base . ExecuteTest ( accessor =>
100101 {
101102 const string commandText = @"SELECT [x].[name], [y].[name], [z].[data]
102103FROM (VALUES (N'feature1')) AS [x]([name])
@@ -115,7 +116,7 @@ INNER JOIN (VALUES (0x1, N'feature1')
115116 } ) ;
116117
117118 [ TestMethod ]
118- public Task QuerySingle_WithAutoMultiMap_NonEntity_SingleProperty_ReferenceEqualityIsPerformed ( ) => base . ExecuteTest ( accessor =>
119+ public Task QuerySingle_WithAutoMultiMap_NonEntityNonPrimitive_SingleProperty_ReferenceEqualityIsPerformed ( ) => base . ExecuteTest ( accessor =>
119120 {
120121 const string commandText = @"SELECT [x].[name], [y].[data], [z].[name]
121122FROM (VALUES (N'product1')) AS [x]([name])
@@ -131,6 +132,42 @@ INNER JOIN (VALUES (N'feature1', N'product1')
131132 Assert . AreEqual ( "feature2" , result . Features [ 1 ] . Name ) ;
132133 } ) ;
133134
135+ [ TestMethod ]
136+ public Task QuerySingle_WithAutoMultiMap_Primitive_Collection_ReferenceEqualityIsPerformed ( ) => base . ExecuteTest ( accessor =>
137+ {
138+ const string commandText = @"SELECT [name] = [x].[feature_name], [name] = [y].[featureitem_name], [z].[featureitem_id]
139+ FROM (VALUES (N'feature1')) AS [x]([feature_name])
140+ INNER JOIN (VALUES (N'black', N'feature1')
141+ , (N'red', N'feature1')) AS [y]([featureitem_name], [feature_name]) ON [x].[feature_name] = [y].[feature_name]
142+ INNER JOIN (VALUES (1, N'feature1')
143+ , (2, N'feature1')) AS [z]([featureitem_id], [feature_name]) ON [x].[feature_name] = [z].[feature_name]" ;
144+ FeatureEntity result = accessor . QuerySingle < FeatureEntity , FeatureItemEntity , int > ( commandText , accessor . Parameters ( ) . Build ( ) , "name,featureitem_id" ) ;
145+ Assert . AreEqual ( "feature1" , result . Name ) ;
146+ Assert . AreEqual ( 2 , result . Items . Count ) ;
147+ Assert . AreEqual ( "black" , result . Items [ 0 ] . Name ) ;
148+ Assert . AreEqual ( "red" , result . Items [ 1 ] . Name ) ;
149+ Assert . AreEqual ( 2 , result . FeatureItemIds . Count ) ;
150+ Assert . AreEqual ( 1 , result . FeatureItemIds [ 0 ] ) ;
151+ Assert . AreEqual ( 2 , result . FeatureItemIds [ 1 ] ) ;
152+ } ) ;
153+
154+ [ TestMethod ]
155+ public Task QuerySingle_WithAutoMultiMap_Primitive_SingleProperty_NoMappingIsPerformed ( ) => base . ExecuteTest ( accessor =>
156+ {
157+ const string commandText = @"SELECT [name] = [x].[product_name], [y].[feature_id], [name] = [z].[feature_name]
158+ FROM (VALUES (N'product1')) AS [x]([product_name])
159+ INNER JOIN (VALUES (1, N'product1')
160+ , (2, N'product1')) AS [y]([feature_id], [product_name]) ON [x].[product_name] = [y].[product_name]
161+ INNER JOIN (VALUES (N'feature1', N'product1')
162+ , (N'feature2', N'product1')) AS [z]([feature_name], [product_name]) ON [x].[product_name] = [z].[product_name]" ;
163+ ProductEntity result = accessor . QuerySingle < ProductEntity , int , FeatureEntity > ( commandText , accessor . Parameters ( ) . Build ( ) , "feature_id,name" ) ;
164+ Assert . AreEqual ( "product1" , result . Name ) ;
165+ Assert . AreEqual ( 0 , result . FirstFeatureId ) ; // Setting a primitive property using multi map doesn't make sense and is therefore not supported
166+ Assert . AreEqual ( 2 , result . Features . Count ) ;
167+ Assert . AreEqual ( "feature1" , result . Features [ 0 ] . Name ) ;
168+ Assert . AreEqual ( "feature2" , result . Features [ 1 ] . Name ) ;
169+ } ) ;
170+
134171 private class RecursiveEntity
135172 {
136173 [ Key ]
@@ -152,13 +189,15 @@ private class FeatureEntity
152189 public IList < FeatureItemEntity > Items { get ; }
153190 public IList < DependentFeatureEntity > Dependencies { get ; }
154191 public IList < byte [ ] > Pictures { get ; }
192+ public IList < int > FeatureItemIds { get ; }
155193
156194 public FeatureEntity ( )
157195 {
158196 this . Translations = new Collection < TranslationEntity > ( ) ;
159197 this . Items = new Collection < FeatureItemEntity > ( ) ;
160198 this . Dependencies = new Collection < DependentFeatureEntity > ( ) ;
161199 this . Pictures = new Collection < byte [ ] > ( ) ;
200+ this . FeatureItemIds = new Collection < int > ( ) ;
162201 }
163202 }
164203
@@ -199,6 +238,7 @@ private class ProductEntity
199238 public string Name { get ; set ; }
200239 public byte [ ] Picture { get ; set ; }
201240 public IList < FeatureEntity > Features { get ; }
241+ public int FirstFeatureId { get ; set ; }
202242
203243 public ProductEntity ( )
204244 {
0 commit comments