• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / EntityFrameworkCore.Generator / 15072048022

16 May 2025 03:31PM UTC coverage: 55.392% (-1.4%) from 56.772%
15072048022

push

github

pwelter34
enable nullable support

616 of 1271 branches covered (48.47%)

Branch coverage included in aggregate %.

233 of 397 new or added lines in 61 files covered. (58.69%)

17 existing lines in 11 files now uncovered.

1824 of 3134 relevant lines covered (58.2%)

88.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.57
/src/EntityFrameworkCore.Generator.Core/Parsing/SourceSynchronizer.cs
1
using EntityFrameworkCore.Generator.Extensions;
2
using EntityFrameworkCore.Generator.Metadata.Generation;
3
using EntityFrameworkCore.Generator.Metadata.Parsing;
4
using EntityFrameworkCore.Generator.Options;
5

6
using Microsoft.Extensions.Logging;
7

8
namespace EntityFrameworkCore.Generator.Parsing;
9

10
public class SourceSynchronizer
11
{
12
    private readonly ILoggerFactory _loggerFactory;
13
    private readonly ILogger _logger;
14

15
    public SourceSynchronizer(ILoggerFactory loggerFactory)
3✔
16
    {
17
        _loggerFactory = loggerFactory;
3✔
18
        _logger = loggerFactory.CreateLogger<SourceSynchronizer>();
3✔
19
    }
3✔
20

21
    public bool UpdateFromSource(EntityContext generatedContext, GeneratorOptions options)
22
    {
23
        if (generatedContext == null)
3!
24
            return false;
×
25

26
        _logger.LogInformation("Parsing existing source for changes ...");
3✔
27

28
        // make sure to update the entities before the context
29
        UpdateFromMapping(generatedContext, options.Data.Mapping.Directory);
3✔
30
        UpdateFromContext(generatedContext, options.Data.Context.Directory);
3✔
31

32
        return true;
3✔
33
    }
34

35

36
    private void UpdateFromContext(EntityContext generatedContext, string? contextDirectory)
37
    {
38
        if (generatedContext == null
3✔
39
            || contextDirectory == null
3✔
40
            || !Directory.Exists(contextDirectory))
3✔
41
        {
42
            return;
1✔
43
        }
44

45
        var parser = new ContextParser(_loggerFactory);
2✔
46

47
        // search all cs files looking for DbContext.  need this in case of context class rename
48
        ParsedContext? parsedContext = null;
2✔
49

50
        using var files = Directory.EnumerateFiles(contextDirectory, "*.cs").GetEnumerator();
2✔
51
        while (files.MoveNext() && parsedContext == null)
4✔
52
            parsedContext = parser.ParseFile(files.Current);
2✔
53

54
        if (parsedContext == null)
2!
55
            return;
×
56

57
        if (generatedContext.ContextClass != parsedContext.ContextClass)
2!
58
        {
59
            _logger.LogInformation(
×
NEW
60
                "Rename Context Class'{ContextClass}' to '{ContextClass}'.",
×
61
                generatedContext.ContextClass,
×
62
                parsedContext.ContextClass);
×
63

64
            generatedContext.ContextClass = parsedContext.ContextClass;
×
65
        }
66

67
        foreach (var parsedProperty in parsedContext.Properties)
48✔
68
        {
69
            var entity = generatedContext.Entities.ByClass(parsedProperty.EntityClass);
22✔
70
            if (entity == null)
22✔
71
                continue;
72

73
            if (entity.ContextProperty == parsedProperty.ContextProperty)
22!
74
                continue;
75

76
            _logger.LogInformation(
×
NEW
77
                "Rename Context Property'{EntityProperty}' to '{ParsedProperty}'.",
×
78
                entity.ContextProperty,
×
79
                parsedProperty.ContextProperty);
×
80

81
            entity.ContextProperty = parsedProperty.ContextProperty;
×
82
        }
83
    }
2✔
84

85
    private void UpdateFromMapping(EntityContext generatedContext, string? mappingDirectory)
86
    {
87
        if (generatedContext == null
3✔
88
            || mappingDirectory == null
3✔
89
            || !Directory.Exists(mappingDirectory))
3✔
90
        {
91
            return;
1✔
92
        }
93

94
        var parser = new MappingParser(_loggerFactory);
2✔
95

96
        // parse all mapping files
97
        var mappingFiles = Directory.EnumerateFiles(mappingDirectory, "*.cs");
2✔
98
        var parsedEntities = mappingFiles
2✔
99
            .Select(parser.ParseFile)
2✔
100
            .Where(parsedEntity => parsedEntity != null)
22✔
101
            .ToList();
2✔
102

103
        // update all entity and property names first because relationships are linked by property names
104
        foreach (var parsedEntity in parsedEntities)
48✔
105
        {
106
            if (parsedEntity == null)
22✔
107
                continue;
108

109
            // find entity by table name to support renaming entity
110
            var entity = generatedContext.Entities
22✔
111
                .ByTable(parsedEntity.TableName, parsedEntity.TableSchema);
22✔
112

113
            if (entity == null)
22✔
114
                continue;
115

116
            // sync names
117
            if (entity.MappingClass != parsedEntity.MappingClass)
22!
118
            {
119
                _logger.LogInformation(
×
NEW
120
                    "  Rename Mapping Class'{EntityClass}' to '{ParsedClass}'.",
×
121
                    entity.MappingClass,
×
122
                    parsedEntity.MappingClass);
×
123

124
                entity.MappingClass = parsedEntity.MappingClass;
×
125
            }
126

127
            RenameEntity(generatedContext, entity.EntityClass, parsedEntity.EntityClass);
22✔
128

129
            // sync properties
130
            foreach (var parsedProperty in parsedEntity.Properties)
464✔
131
            {
132
                // find property by column name to support property name rename
133
                var property = entity.Properties.ByColumn(parsedProperty.ColumnName);
210✔
134
                if (property == null)
210✔
135
                    continue;
136

137
                RenameProperty(
210✔
138
                    generatedContext,
210✔
139
                    entity.EntityClass,
210✔
140
                    property.PropertyName,
210✔
141
                    parsedProperty.PropertyName);
210✔
142
            }
143

144
            // sync relationships
145
            foreach (var parsedRelationship in parsedEntity.Relationships)
68✔
146
            {
147
                var relationship = entity.Relationships.FirstOrDefault(r => r.RelationshipName == parsedRelationship.RelationshipName && r.IsForeignKey);
32✔
148
                if (relationship == null)
12✔
149
                    continue;
150

151
                RenameRelationship(parsedRelationship, relationship);
12✔
152
            }
153
        }
154
    }
2✔
155

156

157

158
    private void RenameEntity(EntityContext generatedContext, string originalName, string newName)
159
    {
160
        if (originalName == newName)
22!
161
            return;
22✔
162

NEW
163
        _logger.LogInformation("  Rename Entity '{OrginalName}' to '{NewName}'.", originalName, newName);
×
164
        foreach (var entity in generatedContext.Entities)
×
165
        {
166
            if (entity.EntityClass == originalName)
×
167
                entity.EntityClass = newName;
×
168
        }
169
    }
×
170

171
    private void RenameProperty(EntityContext generatedContext, string entityName, string originalName, string newName)
172
    {
173
        if (originalName == newName)
210!
174
            return;
210✔
175

NEW
176
        _logger.LogInformation("  Rename Property '{OrginalName}' to '{NewName}' in Entity '{EntityName}'.", originalName, newName, entityName);
×
177
        foreach (var entity in generatedContext.Entities)
×
178
        {
179
            if (entity.EntityClass != entityName)
×
180
                continue;
181

182
            var property = entity.Properties.ByProperty(originalName);
×
183
            if (property != null)
×
184
                property.PropertyName = newName;
×
185
        }
186

187
    }
×
188

189
    private void RenameRelationship(ParsedRelationship parsedRelationship, Relationship relationship)
190
    {
191
        if (parsedRelationship.PropertyName.HasValue() && relationship.PropertyName != parsedRelationship.PropertyName)
12!
192
        {
NEW
193
            _logger.LogInformation("  Rename Property '{OrginalName}' to '{NewName}' in Entity '{EntityName}'.", relationship.PropertyName, parsedRelationship.PropertyName, relationship.Entity.EntityClass);
×
194
            relationship.PropertyName = parsedRelationship.PropertyName;
×
195
        }
196

197
        if (parsedRelationship.PrimaryPropertyName.HasValue() && relationship.PrimaryPropertyName != parsedRelationship.PrimaryPropertyName)
12!
198
        {
NEW
199
            _logger.LogInformation("  Rename Property '{OrginalName}' to '{NewName}' in Entity '{EntityName}'.", relationship.PrimaryPropertyName, parsedRelationship.PrimaryPropertyName, relationship.PrimaryEntity.EntityClass);
×
200
            relationship.PrimaryPropertyName = parsedRelationship.PrimaryPropertyName;
×
201
        }
202

203
        var primaryEntity = relationship.PrimaryEntity;
12✔
204
        var primaryRelationship = primaryEntity.Relationships.FirstOrDefault(r => r.RelationshipName == parsedRelationship.RelationshipName && !r.IsForeignKey);
30✔
205

206
        if (primaryRelationship == null)
12!
207
            return;
×
208

209
        if (parsedRelationship.PrimaryPropertyName.HasValue() && primaryRelationship.PropertyName != parsedRelationship.PrimaryPropertyName)
12!
210
        {
NEW
211
            _logger.LogInformation("  Rename Property '{OrginalName}' to '{NewName}' in Entity '{EntityName}'.", primaryRelationship.PropertyName, parsedRelationship.PrimaryPropertyName, primaryRelationship.Entity.EntityClass);
×
212
            primaryRelationship.PropertyName = parsedRelationship.PrimaryPropertyName;
×
213
        }
214

215
        if (parsedRelationship.PropertyName.HasValue() && primaryRelationship.PrimaryPropertyName != parsedRelationship.PropertyName)
12!
216
        {
NEW
217
            _logger.LogInformation("  Rename Property '{OrginalName}' to '{NewName}' in Entity '{EntityName}'.", primaryRelationship.PrimaryPropertyName, parsedRelationship.PropertyName, primaryRelationship.PrimaryEntity.EntityClass);
×
218
            primaryRelationship.PrimaryPropertyName = parsedRelationship.PropertyName;
×
219
        }
220
    }
12✔
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc