1+ using System ;
2+ using System . Collections . Generic ;
3+ using NUnit . Framework ;
4+ using ServiceStack . DataAnnotations ;
5+ using ServiceStack . Text ;
6+
7+ namespace ServiceStack . OrmLite . Tests . Issues
8+ {
9+ public class Product
10+ {
11+ [ AutoIncrement ]
12+ public int Id { get ; set ; }
13+ public string ProductType { get ; set ; }
14+ public string Name { get ; set ; }
15+ public string Description { get ; set ; }
16+ public int DisplayOrder { get ; set ; }
17+ public bool LimitedToStores { get ; set ; }
18+ public string Sku { get ; set ; }
19+ public decimal Price { get ; set ; }
20+ public decimal OldPrice { get ; set ; }
21+ public decimal SpecialPrice { get ; set ; }
22+ public decimal DiscountPercentage { get ; set ; }
23+ public DateTime ? DateChanged { get ; set ; }
24+ public DateTime ? DateCreated { get ; set ; }
25+
26+ [ Reference ]
27+ public List < StockItem > StockItems { get ; set ; } = new List < StockItem > ( ) ;
28+ }
29+
30+ public class StockItem
31+ {
32+ [ AutoIncrement ]
33+ public int Id { get ; set ; }
34+ [ References ( typeof ( Product ) ) ]
35+ public int ProductId { get ; set ; }
36+ public string Size { get ; set ; }
37+ public int TotalStockQuantity { get ; set ; }
38+ public string Gtin { get ; set ; }
39+ public int DisplayOrder { get ; set ; }
40+
41+ [ Reference ]
42+ public Product Product { get ; set ; }
43+ }
44+
45+ public class AutoQueryJoinTests : OrmLiteTestBase
46+ {
47+ [ Test ]
48+ public void Can_select_references_with_join ( )
49+ {
50+ using ( var db = OpenDbConnection ( ) )
51+ {
52+ db . DropTable < StockItem > ( ) ;
53+ db . DropTable < Product > ( ) ;
54+ db . CreateTable < Product > ( ) ;
55+ db . CreateTable < StockItem > ( ) ;
56+
57+ db . Save ( new Product
58+ {
59+ ProductType = "A" ,
60+ Name = "Name A" ,
61+ DisplayOrder = 1 ,
62+ Sku = "SKU A" ,
63+ Price = 1 ,
64+ DateCreated = DateTime . UtcNow ,
65+ StockItems = new List < StockItem >
66+ {
67+ new StockItem { Size = "1" , TotalStockQuantity = 1 , DisplayOrder = 1 } ,
68+ new StockItem { Size = "2" , TotalStockQuantity = 2 , DisplayOrder = 2 } ,
69+ }
70+ } , references : true ) ;
71+
72+ db . Save ( new Product
73+ {
74+ ProductType = "B" ,
75+ Name = "Name B" ,
76+ DisplayOrder = 2 ,
77+ Sku = "SKU B" ,
78+ Price = 2 ,
79+ DateCreated = DateTime . UtcNow ,
80+ StockItems = new List < StockItem >
81+ {
82+ new StockItem { Size = "3" , TotalStockQuantity = 3 , DisplayOrder = 3 } ,
83+ new StockItem { Size = "4" , TotalStockQuantity = 4 , DisplayOrder = 4 } ,
84+ }
85+ } , references : true ) ;
86+
87+ db . Insert ( new Product
88+ {
89+ ProductType = "C" ,
90+ Name = "Name C" ,
91+ DisplayOrder = 3 ,
92+ Sku = "SKU C" ,
93+ Price = 3 ,
94+ DateCreated = DateTime . UtcNow ,
95+ } ) ;
96+
97+ var results = db . LoadSelect < Product > ( ) ;
98+ Assert . That ( results . Count , Is . EqualTo ( 3 ) ) ;
99+
100+ var q = db . From < Product > ( ) . Join < StockItem > ( ) ;
101+ var products = db . Select ( q . SelectDistinct ( ) ) ;
102+ var stockItems = db . Select < StockItem > ( ) ;
103+
104+ products . Merge ( stockItems ) ;
105+
106+ Assert . That ( products . Count , Is . EqualTo ( 2 ) ) ;
107+ Assert . That ( products [ 0 ] . StockItems . Count , Is . EqualTo ( 2 ) ) ;
108+ Assert . That ( products [ 1 ] . StockItems . Count , Is . EqualTo ( 2 ) ) ;
109+ }
110+ }
111+ }
112+ }
0 commit comments