@@ -50,19 +50,78 @@ public class Order
5050 public decimal Cost { get ; set ; }
5151 }
5252
53+ /// <summary>
54+ /// Test POCOs using table aliases and an alias on the foreign key reference
55+ /// </summary>
56+ [ Alias ( "Q_Customer" ) ]
57+ public class AliasedCustomer
58+ {
59+ [ AutoIncrement ]
60+ public int Id { get ; set ; }
61+ public string Name { get ; set ; }
62+
63+ [ Reference ]
64+ public AliasedCustomerAddress PrimaryAddress { get ; set ; }
65+ }
66+
67+ [ Alias ( "Q_CustomerAddress" ) ]
68+ public class AliasedCustomerAddress
69+ {
70+ [ AutoIncrement ]
71+ public int Id { get ; set ; }
72+ [ Alias ( "Q_CustomerId" ) ]
73+ public int AliasedCustomerId { get ; set ; }
74+ public string AddressLine1 { get ; set ; }
75+ public string AddressLine2 { get ; set ; }
76+ public string City { get ; set ; }
77+ public string State { get ; set ; }
78+ public string Country { get ; set ; }
79+ }
80+
81+ /// <summary>
82+ /// Test POCOs using table aliases and old form foreign key reference which was aliased name
83+ /// </summary>
84+ [ Alias ( "QO_Customer" ) ]
85+ public class OldAliasedCustomer
86+ {
87+ [ AutoIncrement ]
88+ public int Id { get ; set ; }
89+ public string Name { get ; set ; }
90+
91+ [ Reference ]
92+ public OldAliasedCustomerAddress PrimaryAddress { get ; set ; }
93+ }
94+
95+ [ Alias ( "QO_CustomerAddress" ) ]
96+ public class OldAliasedCustomerAddress
97+ {
98+ [ AutoIncrement ]
99+ public int Id { get ; set ; }
100+ public int QO_CustomerId { get ; set ; }
101+ public string AddressLine1 { get ; set ; }
102+ public string AddressLine2 { get ; set ; }
103+ public string City { get ; set ; }
104+ public string State { get ; set ; }
105+ public string Country { get ; set ; }
106+ }
107+
53108 public class LoadReferencesTests
54109 : OrmLiteTestBase
55110 {
56111 private IDbConnection db ;
57112
58113 [ TestFixtureSetUp ]
59- public void TestFixtureSetUp ( )
114+ public new void TestFixtureSetUp ( )
60115 {
61116 db = base . OpenDbConnection ( ) ;
62117 db . DropTable < OrderDetail > ( ) ;
63118 db . DropAndCreateTable < Order > ( ) ;
64119 db . DropAndCreateTable < Customer > ( ) ;
65120 db . DropAndCreateTable < CustomerAddress > ( ) ;
121+ db . DropAndCreateTable < AliasedCustomer > ( ) ;
122+ db . DropAndCreateTable < AliasedCustomerAddress > ( ) ;
123+ db . DropAndCreateTable < OldAliasedCustomer > ( ) ;
124+ db . DropAndCreateTable < OldAliasedCustomerAddress > ( ) ;
66125 }
67126
68127 [ TestFixtureTearDown ]
@@ -117,6 +176,66 @@ public void Can_Save_and_Load_References()
117176 Assert . That ( dbCustomer . Orders . Count , Is . EqualTo ( 2 ) ) ;
118177 }
119178
179+ [ Test ]
180+ public void Can_Save_and_Load_Aliased_References ( )
181+ {
182+ var customer = new AliasedCustomer
183+ {
184+ Name = "Customer 1" ,
185+ PrimaryAddress = new AliasedCustomerAddress
186+ {
187+ AddressLine1 = "1 Humpty Street" ,
188+ City = "Humpty Doo" ,
189+ State = "Northern Territory" ,
190+ Country = "Australia"
191+ } ,
192+ } ;
193+
194+ db . Save ( customer ) ;
195+
196+ Assert . That ( customer . Id , Is . GreaterThan ( 0 ) ) ;
197+ Assert . That ( customer . PrimaryAddress . AliasedCustomerId , Is . EqualTo ( 0 ) ) ;
198+
199+ db . SaveReferences ( customer , customer . PrimaryAddress ) ;
200+ Assert . That ( customer . PrimaryAddress . AliasedCustomerId , Is . EqualTo ( customer . Id ) ) ;
201+
202+ var dbCustomer = db . LoadSingleById < AliasedCustomer > ( customer . Id ) ;
203+
204+ dbCustomer . PrintDump ( ) ;
205+
206+ Assert . That ( dbCustomer . PrimaryAddress , Is . Not . Null ) ;
207+ }
208+
209+ [ Test ]
210+ public void Can_Save_and_Load_Old_Aliased_References ( )
211+ {
212+ var customer = new OldAliasedCustomer
213+ {
214+ Name = "Customer 1" ,
215+ PrimaryAddress = new OldAliasedCustomerAddress
216+ {
217+ AddressLine1 = "1 Humpty Street" ,
218+ City = "Humpty Doo" ,
219+ State = "Northern Territory" ,
220+ Country = "Australia"
221+ } ,
222+ } ;
223+
224+ db . Save ( customer ) ;
225+
226+ Assert . That ( customer . Id , Is . GreaterThan ( 0 ) ) ;
227+ Assert . That ( customer . PrimaryAddress . QO_CustomerId , Is . EqualTo ( 0 ) ) ;
228+
229+ db . SaveReferences ( customer , customer . PrimaryAddress ) ;
230+ Assert . That ( customer . PrimaryAddress . QO_CustomerId , Is . EqualTo ( customer . Id ) ) ;
231+
232+ var dbCustomer = db . LoadSingleById < OldAliasedCustomer > ( customer . Id ) ;
233+
234+ dbCustomer . PrintDump ( ) ;
235+
236+ Assert . That ( dbCustomer . PrimaryAddress , Is . Not . Null ) ;
237+ }
238+
120239 [ Test ]
121240 public void Can_SaveAllReferences_then_Load_them ( )
122241 {
0 commit comments