1+ using System ;
2+ using System . Data ;
3+ using NUnit . Framework ;
4+ using ServiceStack . DataAnnotations ;
5+ using ServiceStack . Text ;
6+
7+ namespace ServiceStack . OrmLite . Tests . Issues
8+ {
9+ public class SaveAllReferencesIssues : OrmLiteTestBase
10+ {
11+ public class BranchRef
12+ {
13+ [ AutoId ]
14+ public Guid Id { get ; set ; }
15+
16+ [ Reference ]
17+ public AddressRef Address { get ; set ; }
18+ }
19+
20+ public class AddressRef
21+ {
22+ [ AutoId ]
23+ public Guid Id { get ; set ; }
24+
25+ [ ForeignKey ( typeof ( BranchRef ) , OnDelete = "CASCADE" ) ]
26+ [ Required ]
27+ public Guid BranchRefId { get ; set ; }
28+
29+ [ Required ]
30+ public string StreetAddress { get ; set ; }
31+
32+ [ Required ]
33+ public string City { get ; set ; }
34+
35+ [ Required ]
36+ public string State { get ; set ; }
37+
38+ [ Required ]
39+ public string ZipCode { get ; set ; }
40+ }
41+
42+ private static void CreateRefTables ( IDbConnection db )
43+ {
44+ if ( db . TableExists < BranchRef > ( ) )
45+ db . DeleteAll < BranchRef > ( ) ;
46+
47+ if ( db . TableExists < AddressRef > ( ) )
48+ db . DeleteAll < AddressRef > ( ) ;
49+
50+ db . DropTable < AddressRef > ( ) ;
51+ db . DropTable < BranchRef > ( ) ;
52+
53+ db . CreateTable < BranchRef > ( ) ;
54+ db . CreateTable < AddressRef > ( ) ;
55+ }
56+
57+ [ Test ]
58+ public void Can_use_Save_References_with_ForeignKey ( )
59+ {
60+ OrmLiteConfig . BeforeExecFilter = cmd => cmd . GetDebugString ( ) . Print ( ) ;
61+
62+ using ( var db = OpenDbConnection ( ) )
63+ {
64+ CreateRefTables ( db ) ;
65+
66+ //Generate dummy data
67+ var branch = new BranchRef
68+ {
69+ Address = new AddressRef
70+ {
71+ StreetAddress = "2100 Gotham Lane" ,
72+ City = "Gotham" ,
73+ State = "NJ" ,
74+ ZipCode = "12345"
75+ }
76+ } ;
77+
78+ db . Save ( branch , references : true ) ;
79+
80+ Assert . That ( branch . Id , Is . Not . EqualTo ( Guid . Empty ) ) ;
81+ Assert . That ( branch . Address . Id , Is . Not . EqualTo ( Guid . Empty ) ) ;
82+ Assert . That ( branch . Id , Is . EqualTo ( branch . Address . BranchRefId ) ) ;
83+ }
84+ }
85+
86+ public class BranchSelfRef
87+ {
88+ [ AutoId ]
89+ public Guid Id { get ; set ; }
90+
91+ [ Reference ]
92+ public AddressSelfRef Address { get ; set ; }
93+
94+ [ ForeignKey ( typeof ( AddressSelfRef ) , OnDelete = "CASCADE" ) ]
95+ public Guid ? AddressSelfRefId { get ; set ; }
96+ }
97+
98+ public class AddressSelfRef
99+ {
100+ [ AutoId ]
101+ public Guid Id { get ; set ; }
102+
103+ [ Required ]
104+ public string StreetAddress { get ; set ; }
105+
106+ [ Required ]
107+ public string City { get ; set ; }
108+
109+ [ Required ]
110+ public string State { get ; set ; }
111+
112+ [ Required ]
113+ public string ZipCode { get ; set ; }
114+ }
115+
116+ private static void CreateSelfRefTables ( IDbConnection db )
117+ {
118+ if ( db . TableExists < AddressSelfRef > ( ) )
119+ db . DeleteAll < AddressSelfRef > ( ) ;
120+
121+ if ( db . TableExists < BranchSelfRef > ( ) )
122+ db . DeleteAll < BranchSelfRef > ( ) ;
123+
124+ db . DropTable < BranchSelfRef > ( ) ;
125+ db . DropTable < AddressSelfRef > ( ) ;
126+
127+ db . CreateTable < AddressSelfRef > ( ) ;
128+ db . CreateTable < BranchSelfRef > ( ) ;
129+ }
130+
131+ [ Test ]
132+ public void Can_use_Save_References_with_ForeignKey_using_Self_Reference_Id ( )
133+ {
134+ OrmLiteConfig . BeforeExecFilter = cmd => cmd . GetDebugString ( ) . Print ( ) ;
135+
136+ using ( var db = OpenDbConnection ( ) )
137+ {
138+ CreateSelfRefTables ( db ) ;
139+
140+ var branch = new BranchSelfRef
141+ {
142+ Address = new AddressSelfRef
143+ {
144+ StreetAddress = "2100 Gotham Lane" ,
145+ City = "Gotham" ,
146+ State = "NJ" ,
147+ ZipCode = "12345"
148+ }
149+ } ;
150+
151+ db . Save ( branch , references : true ) ;
152+
153+ Assert . That ( branch . Id , Is . Not . EqualTo ( Guid . Empty ) ) ;
154+ Assert . That ( branch . AddressSelfRefId , Is . Not . EqualTo ( Guid . Empty ) ) ;
155+ Assert . That ( branch . AddressSelfRefId , Is . EqualTo ( branch . Address . Id ) ) ;
156+ }
157+ }
158+
159+ }
160+ }
0 commit comments