1+ using System . Collections . Generic ;
2+ using System . Threading . Tasks ;
3+ using NUnit . Framework ;
4+ using ServiceStack . DataAnnotations ;
5+
6+ namespace ServiceStack . OrmLite . Tests . Issues
7+ {
8+ [ TestFixtureOrmLiteDialects ( Dialect . Sqlite ) ]
9+ public class LoadReferenceIssueWithCyclicalForeignKeys : OrmLiteProvidersTestBase
10+ {
11+ public LoadReferenceIssueWithCyclicalForeignKeys ( DialectContext context ) : base ( context ) { }
12+
13+ public class BaseEntity
14+ {
15+ [ AutoIncrement ]
16+ [ PrimaryKey ]
17+ public long Id { get ; set ; }
18+ }
19+
20+ public class ResearchEntity : BaseEntity
21+ {
22+ [ References ( typeof ( NameEntity ) ) ]
23+ public long ? PrimaryNameId { get ; set ; }
24+
25+ [ Reference ]
26+ public NameEntity PrimaryName { get ; set ; }
27+
28+ [ Reference ]
29+ public List < NameEntity > Names { get ; set ; } = new List < NameEntity > ( ) ;
30+ }
31+
32+ public class NameEntity : BaseEntity
33+ {
34+ public string Value { get ; set ; }
35+
36+ [ References ( typeof ( ResearchEntity ) ) ]
37+ public long ResearchId { get ; set ; }
38+
39+ [ Reference ]
40+ public ResearchEntity Research { get ; set ; }
41+ }
42+
43+ [ Test ]
44+ public void Does_update_self_FK_Key_when_saving_references ( )
45+ {
46+ using var db = OpenDbConnection ( ) ;
47+ db . DropAndCreateTable < NameEntity > ( ) ;
48+ db . DropAndCreateTable < ResearchEntity > ( ) ;
49+
50+ for ( var i = 1 ; i <= 5 ; i ++ )
51+ {
52+ var research = new ResearchEntity ( ) ;
53+ research . Names . Add ( new NameEntity { Value = $ "test { 1 + i } "} ) ;
54+ research . Names . Add ( new NameEntity { Value = $ "test { 2 + i } "} ) ;
55+ research . Names . Add ( new NameEntity { Value = $ "test { 3 + i } "} ) ;
56+
57+ db . Save ( research , references : true ) ;
58+ research . PrimaryNameId = research . Names [ 1 ] . Id ;
59+ db . Save ( research ) ;
60+ }
61+
62+ OrmLiteUtils . PrintSql ( ) ;
63+ var res = db . LoadSelect (
64+ db . From < ResearchEntity > ( ) . Where ( x => x . Id == 5 ) )
65+ . FirstNonDefault ( ) ;
66+ Assert . That ( res . PrimaryName . Id , Is . EqualTo ( res . PrimaryNameId ) ) ;
67+ }
68+
69+ [ Test ]
70+ public async Task Does_update_self_FK_Key_when_saving_references_Async ( )
71+ {
72+ using var db = await OpenDbConnectionAsync ( ) ;
73+ db . DropAndCreateTable < NameEntity > ( ) ;
74+ db . DropAndCreateTable < ResearchEntity > ( ) ;
75+
76+ for ( var i = 1 ; i <= 5 ; i ++ )
77+ {
78+ var research = new ResearchEntity ( ) ;
79+ research . Names . Add ( new NameEntity { Value = $ "test { 1 + i } "} ) ;
80+ research . Names . Add ( new NameEntity { Value = $ "test { 2 + i } "} ) ;
81+ research . Names . Add ( new NameEntity { Value = $ "test { 3 + i } "} ) ;
82+
83+ await db . SaveAsync ( research , references : true ) ;
84+ research . PrimaryNameId = research . Names [ 1 ] . Id ;
85+ await db . SaveAsync ( research ) ;
86+ }
87+
88+ OrmLiteUtils . PrintSql ( ) ;
89+ var res = ( await db . LoadSelectAsync (
90+ db . From < ResearchEntity > ( ) . Where ( x => x . Id == 5 ) ) )
91+ . FirstNonDefault ( ) ;
92+ Assert . That ( res . PrimaryName . Id , Is . EqualTo ( res . PrimaryNameId ) ) ;
93+ }
94+ }
95+ }
0 commit comments