1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading . Tasks ;
24using NUnit . Framework ;
35using ServiceStack . DataAnnotations ;
46using ServiceStack . Text ;
@@ -46,5 +48,88 @@ public void Does_populate_Ref_Ids_of_non_convention_PrimaryKey_Tables()
4648 Assert . That ( root . Items [ 0 ] . RootId , Is . EqualTo ( root . RootId ) ) ;
4749 }
4850 }
51+
52+ [ Alias ( "Users" ) ]
53+ public class User
54+ {
55+ [ AutoId ]
56+ public Guid Id { get ; set ; }
57+
58+ [ Reference ]
59+ public List < UserBranch > Branches { get ; set ; }
60+
61+ [ Reference ]
62+ public UserMeta Meta { get ; set ; }
63+
64+ [ Reference ]
65+ public List < UserAddress > Addresses { get ; set ; }
66+ }
67+
68+ [ Alias ( "UserMetas" ) ]
69+ public class UserMeta
70+ {
71+ [ PrimaryKey ]
72+ [ ForeignKey ( typeof ( User ) , OnDelete = "CASCADE" , OnUpdate = "CASCADE" ) ]
73+ [ References ( typeof ( User ) ) ]
74+ public Guid UserId { get ; set ; }
75+ }
76+
77+ [ Alias ( "UserBranches" ) ]
78+ public class UserBranch
79+ {
80+ [ AutoId ]
81+ public Guid Id { get ; set ; }
82+
83+ [ ForeignKey ( typeof ( User ) , OnDelete = "CASCADE" , OnUpdate = "CASCADE" ) ]
84+ public Guid UserId { get ; set ; }
85+
86+ public string Details { get ; set ; }
87+ }
88+
89+ [ Alias ( "UserAddresses" ) ]
90+ public class UserAddress
91+ {
92+ [ AutoId ]
93+ public Guid Id { get ; set ; }
94+
95+ [ ForeignKey ( typeof ( User ) , OnDelete = "CASCADE" , OnUpdate = "CASCADE" ) ]
96+ public Guid UserId { get ; set ; }
97+
98+ public string Details { get ; set ; }
99+ }
100+
101+ [ Test ]
102+ public async Task Can_create_tables_with_multiple_references ( )
103+ {
104+ using ( var db = await OpenDbConnectionAsync ( ) )
105+ {
106+ db . DropTable < UserMeta > ( ) ;
107+ db . DropTable < UserAddress > ( ) ;
108+ db . DropTable < UserBranch > ( ) ;
109+ db . DropTable < User > ( ) ;
110+
111+ db . CreateTable < User > ( ) ;
112+ db . CreateTable < UserBranch > ( ) ;
113+ db . CreateTable < UserAddress > ( ) ;
114+ db . CreateTable < UserMeta > ( ) ;
115+ }
116+
117+ var userMeta = new UserMeta ( ) ;
118+ var user = new User
119+ {
120+ Meta = userMeta
121+ } ;
122+
123+ using ( var db = await OpenDbConnectionAsync ( ) )
124+ {
125+ user . Branches = new List < UserBranch > { new UserBranch { UserId = user . Id } } ;
126+ user . Addresses = new List < UserAddress > { new UserAddress { UserId = user . Id } } ;
127+
128+ await db . SaveAsync ( user , references : true ) ;
129+
130+ var fromDb = await db . LoadSingleByIdAsync < User > ( user . Id ) ;
131+ fromDb . Dump ( ) . Print ( ) ;
132+ }
133+ }
49134 }
50135}
0 commit comments