@@ -15,7 +15,6 @@ public class ContextGraphBuilder<T> where T : DbContext
1515 public ContextGraph < T > Build ( )
1616 {
1717 _getFirstLevelEntities ( ) ;
18- _loadRelationships ( ) ;
1918
2019 var graph = new ContextGraph < T >
2120 {
@@ -42,7 +41,8 @@ private void _getFirstLevelEntities()
4241 entities . Add ( new ContextEntity {
4342 EntityName = property . Name ,
4443 EntityType = entityType ,
45- Attributes = _getAttributes ( entityType )
44+ Attributes = _getAttributes ( entityType ) ,
45+ Relationships = _getRelationships ( entityType )
4646 } ) ;
4747 }
4848 }
@@ -66,38 +66,29 @@ private List<AttrAttribute> _getAttributes(Type entityType)
6666 return attributes ;
6767 }
6868
69- private void _loadRelationships ( )
70- {
71- _entities . ForEach ( entity => {
72-
73- var relationships = new List < Relationship > ( ) ;
74- var properties = entity . EntityType . GetProperties ( ) ;
75-
76- foreach ( var entityProperty in properties )
77- {
78- var propertyType = entityProperty . PropertyType ;
79-
80- if ( _isValidEntity ( propertyType )
81- || ( propertyType . GetTypeInfo ( ) . IsGenericType && _isValidEntity ( propertyType . GetGenericArguments ( ) [ 0 ] ) ) )
82- relationships . Add ( _getRelationshipFromPropertyInfo ( entityProperty ) ) ;
83- }
84-
85- entity . Relationships = relationships ;
86- } ) ;
87- }
88-
89- private bool _isValidEntity ( Type type )
69+ private List < RelationshipAttribute > _getRelationships ( Type entityType )
9070 {
91- var validEntityRelationshipTypes = _entities . Select ( e => e . EntityType ) ;
92- return validEntityRelationshipTypes . Contains ( type ) ;
71+ var attributes = new List < RelationshipAttribute > ( ) ;
72+
73+ var properties = entityType . GetProperties ( ) ;
74+
75+ foreach ( var prop in properties )
76+ {
77+ var attribute = ( RelationshipAttribute ) prop . GetCustomAttribute ( typeof ( RelationshipAttribute ) ) ;
78+ if ( attribute == null ) continue ;
79+ attribute . InternalRelationshipName = prop . Name ;
80+ attribute . Type = _getRelationshipType ( attribute , prop ) ;
81+ attributes . Add ( attribute ) ;
82+ }
83+ return attributes ;
9384 }
9485
95- private Relationship _getRelationshipFromPropertyInfo ( PropertyInfo propertyInfo )
86+ private Type _getRelationshipType ( RelationshipAttribute relation , PropertyInfo prop )
9687 {
97- return new Relationship {
98- Type = propertyInfo . PropertyType ,
99- RelationshipName = propertyInfo . Name
100- } ;
88+ if ( relation . IsHasMany )
89+ return prop . PropertyType . GetGenericArguments ( ) [ 0 ] ;
90+ else
91+ return prop . PropertyType ;
10192 }
10293 }
10394}
0 commit comments