7
7
using Microsoft . EntityFrameworkCore . Metadata . Builders ;
8
8
using Microsoft . EntityFrameworkCore . Metadata . Conventions . Infrastructure ;
9
9
using Microsoft . EntityFrameworkCore . Metadata . Internal ;
10
+ using Microsoft . EntityFrameworkCore . Utilities ;
10
11
11
12
namespace Microsoft . EntityFrameworkCore . Metadata . Conventions
12
13
{
@@ -30,7 +31,8 @@ public ModelCleanupConvention([NotNull] ProviderConventionSetBuilderDependencies
30
31
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get ; }
31
32
32
33
/// <inheritdoc />
33
- public virtual void ProcessModelFinalizing ( IConventionModelBuilder modelBuilder , IConventionContext < IConventionModelBuilder > context )
34
+ public virtual void ProcessModelFinalizing (
35
+ IConventionModelBuilder modelBuilder , IConventionContext < IConventionModelBuilder > context )
34
36
{
35
37
RemoveEntityTypesUnreachableByNavigations ( modelBuilder , context ) ;
36
38
RemoveNavigationlessForeignKeys ( modelBuilder ) ;
@@ -44,7 +46,7 @@ private void RemoveEntityTypesUnreachableByNavigations(
44
46
var rootEntityTypes = GetRoots ( model , ConfigurationSource . DataAnnotation ) ;
45
47
using ( context . DelayConventions ( ) )
46
48
{
47
- foreach ( var orphan in new ModelNavigationsGraphAdapter ( model ) . GetUnreachableVertices ( rootEntityTypes ) )
49
+ foreach ( var orphan in new GraphAdapter ( model ) . GetUnreachableVertices ( rootEntityTypes ) )
48
50
{
49
51
modelBuilder . HasNoEntityType ( orphan , fromDataAnnotation : true ) ;
50
52
}
@@ -90,5 +92,28 @@ private void RemoveModelBuildingAnnotations(IConventionModelBuilder modelBuilder
90
92
entityType . RemoveAnnotation ( CoreAnnotationNames . NavigationCandidates ) ;
91
93
}
92
94
}
95
+
96
+ private sealed class GraphAdapter : Graph < IConventionEntityType >
97
+ {
98
+ private readonly IConventionModel _model ;
99
+
100
+ public GraphAdapter ( [ NotNull ] IConventionModel model )
101
+ {
102
+ _model = model ;
103
+ }
104
+
105
+ public override IEnumerable < IConventionEntityType > Vertices => _model . GetEntityTypes ( ) ;
106
+
107
+ public override IEnumerable < IConventionEntityType > GetOutgoingNeighbors ( IConventionEntityType from )
108
+ => from . GetForeignKeys ( ) . Where ( fk => fk . DependentToPrincipal != null ) . Select ( fk => fk . PrincipalEntityType )
109
+ . Union (
110
+ from . GetReferencingForeignKeys ( ) . Where ( fk => fk . PrincipalToDependent != null ) . Select ( fk => fk . DeclaringEntityType ) ) ;
111
+
112
+ public override IEnumerable < IConventionEntityType > GetIncomingNeighbors ( IConventionEntityType to )
113
+ => to . GetForeignKeys ( ) . Where ( fk => fk . PrincipalToDependent != null ) . Select ( fk => fk . PrincipalEntityType )
114
+ . Union (
115
+ to . GetReferencingForeignKeys ( ) . Where ( fk => fk . DependentToPrincipal != null ) . Select ( fk => fk . DeclaringEntityType ) ) ;
116
+ }
93
117
}
94
118
}
119
+
0 commit comments