11using JetBrains . Annotations ;
22using Microsoft . EntityFrameworkCore ;
3+ using Microsoft . EntityFrameworkCore . Metadata ;
34
45namespace BitzArt . XDoc ;
56
@@ -14,61 +15,82 @@ public static class ModelBuilderExtensions
1415 /// </summary>
1516 /// <param name="modelBuilder"></param>
1617 /// <param name="xDoc"></param>
17- public static void ConfigureComments ( this ModelBuilder modelBuilder , XDoc xDoc )
18+ public static ModelBuilder ConfigureComments ( this ModelBuilder modelBuilder , XDoc xDoc )
1819 {
1920 var entityTypes = modelBuilder . Model . GetEntityTypes ( ) ;
2021
2122 foreach ( var entityType in entityTypes )
2223 {
23- var typeDocumentation = xDoc . Get ( entityType . ClrType ) ;
24+ ConfigureEntity ( xDoc , entityType ) ;
25+ }
2426
25- if ( typeDocumentation is null )
26- {
27- continue ;
28- }
27+ return modelBuilder ;
28+ }
2929
30- var entityComment = typeDocumentation . ToPlainText ( ) ;
30+ private static void ConfigureEntity ( XDoc xDoc , IMutableEntityType entityType )
31+ {
32+ ConfigureEntityTypeComment ( xDoc , entityType ) ;
3133
32- // For owned entities, we don't set the comment on the entity itself
33- // But we will set the comment on the properties
34+ var properties = entityType . GetProperties ( ) ;
3435
35- var isOwned = entityType . IsOwned ( ) ;
36- var tableName = entityType . GetTableName ( ) ;
36+ foreach ( var property in properties )
37+ {
38+ ConfigureEntityPropertyComment ( xDoc , entityType , property ) ;
39+ }
40+ }
3741
38- if ( ! isOwned && tableName is not null )
39- {
40- entityType . SetComment ( entityComment ) ;
41- }
42+ private static void ConfigureEntityTypeComment ( XDoc xDoc , IMutableEntityType entityType )
43+ {
44+ var typeDocumentation = xDoc . Get ( entityType . ClrType ) ;
4245
43- var properties = entityType . GetProperties ( ) ;
46+ if ( typeDocumentation is null )
47+ {
48+ // No own xml-documentation
49+ return ;
50+ }
51+
52+ var isOwned = entityType . IsOwned ( ) ;
53+ var tableName = entityType . GetTableName ( ) ;
4454
45- foreach ( var property in properties )
46- {
47- var isShadowProperty = property . IsShadowProperty ( ) ;
55+ if ( isOwned || tableName is null )
56+ {
57+ // For owned entities, we don't set the comment on the entity itself
58+ // But we will set the comment on the properties
59+
60+ return ;
61+ }
62+
63+ var entityComment = typeDocumentation . ToPlainText ( ) ;
64+
65+ entityType . SetComment ( entityComment ) ;
66+ }
67+
68+ private static void ConfigureEntityPropertyComment ( XDoc xDoc , IMutableEntityType entityType , IMutableProperty property )
69+ {
70+ var isShadowProperty = property . IsShadowProperty ( ) ;
4871
49- if ( isShadowProperty )
50- {
51- continue ;
52- }
72+ if ( isShadowProperty )
73+ {
74+ return ;
75+ }
5376
54- var propertyInfo = entityType . ClrType . GetProperty ( property . Name ) ;
77+ var propertyInfo = entityType . ClrType . GetProperty ( property . Name ) ;
5578
56- if ( propertyInfo is null )
57- {
58- continue ;
59- }
79+ if ( propertyInfo is null )
80+ {
81+ return ;
82+ }
6083
61- var propertyDocumentation = xDoc . Get ( propertyInfo ) ;
84+ var propertyDocumentation = xDoc . Get ( propertyInfo ) ;
6285
63- if ( propertyDocumentation is null )
64- {
65- continue ;
66- }
86+ if ( propertyDocumentation is null )
87+ {
88+ return ;
89+ }
6790
68- var propertyComment = propertyDocumentation . ToPlainText ( ) ;
91+ var propertyComment = propertyDocumentation . ToPlainText ( ) ;
6992
70- property . SetComment ( propertyComment ) ;
71- }
72- }
93+ property . SetComment ( propertyComment ) ;
7394 }
95+
7496}
0 commit comments