1
+ using System . Collections . Generic ;
2
+ using System . Data . Entity . Core . Metadata . Edm ;
3
+ using System . Linq ;
4
+ using SQLite . CodeFirst . Builder . NameCreators ;
5
+
6
+ namespace SQLite . CodeFirst . Utility
7
+ {
8
+ internal class SqliteAssociationType
9
+ {
10
+ private const string SelfReferencingPostfix = "Self" ;
11
+
12
+ public SqliteAssociationType ( AssociationType associationType , EntityContainer container )
13
+ {
14
+ FromRoleEntitySetName = associationType . Constraint . FromRole . Name ;
15
+ ToRoleEntitySetName = associationType . Constraint . ToRole . Name ;
16
+
17
+ string fromTable = container . GetEntitySetByName ( FromRoleEntitySetName , true ) . Table ;
18
+ string toTable ;
19
+
20
+ if ( IsSelfReferencing ( associationType ) )
21
+ {
22
+ toTable = fromTable ;
23
+ ToRoleEntitySetName = FromRoleEntitySetName ;
24
+ }
25
+ else
26
+ {
27
+ toTable = container . GetEntitySetByName ( ToRoleEntitySetName , true ) . Table ;
28
+ }
29
+
30
+ FromTableName = TableNameCreator . CreateTableName ( fromTable ) ;
31
+ ToTableName = TableNameCreator . CreateTableName ( toTable ) ;
32
+ ForeignKey = associationType . Constraint . ToProperties . Select ( x => x . Name ) ;
33
+ ForeignPrimaryKey = associationType . Constraint . FromProperties . Select ( x => x . Name ) ;
34
+ CascadeDelete = associationType . Constraint . FromRole . DeleteBehavior == OperationAction . Cascade ;
35
+ }
36
+
37
+ private static bool IsSelfReferencing ( AssociationType associationType )
38
+ {
39
+ return associationType . Constraint . ToRole . Name . Remove ( associationType . Constraint . ToRole . Name . Length - SelfReferencingPostfix . Length , SelfReferencingPostfix . Length ) == associationType . Constraint . FromRole . Name ;
40
+ }
41
+
42
+ public string ToRoleEntitySetName { get ; set ; }
43
+ public string FromRoleEntitySetName { get ; set ; }
44
+ public IEnumerable < string > ForeignKey { get ; }
45
+ public string FromTableName { get ; }
46
+ public string ToTableName { get ; }
47
+ public IEnumerable < string > ForeignPrimaryKey { get ; }
48
+ public bool CascadeDelete { get ; }
49
+ }
50
+ }
0 commit comments