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

loresoft / EntityFrameworkCore.Generator / 27767067052

18 Jun 2026 02:35PM UTC coverage: 70.481% (-4.9%) from 75.418%
27767067052

push

github

pwelter34
Update SourceSynchronizer.cs

913 of 1785 branches covered (51.15%)

Branch coverage included in aggregate %.

5 of 6 new or added lines in 1 file covered. (83.33%)

297 existing lines in 10 files now uncovered.

4094 of 5319 relevant lines covered (76.97%)

1291.15 hits per line

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

88.51
/src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs
1
using EntityFrameworkCore.Generator.Extensions;
2
using EntityFrameworkCore.Generator.Metadata.Generation;
3
using EntityFrameworkCore.Generator.Options;
4

5
namespace EntityFrameworkCore.Generator.Templates;
6

7
public class ModelClassTemplate : CodeTemplateBase
8
{
9
    private readonly Model _model;
10

11
    public ModelClassTemplate(Model model, GeneratorOptions options) : base(options)
396✔
12
    {
13
        _model = model;
396✔
14
    }
396✔
15

16
    public override string WriteCode()
17
    {
18
        CodeBuilder.Clear();
396✔
19

20
        if (_model.ModelHeader.HasValue())
396!
21
            CodeBuilder.AppendLine(_model.ModelHeader).AppendLine();
×
22

23
        CodeBuilder.AppendLine("using System;");
396✔
24
        CodeBuilder.AppendLine("using System.Collections.Generic;");
396✔
25
        CodeBuilder.AppendLine();
396✔
26

27
        CodeBuilder.Append($"namespace {_model.ModelNamespace}");
396✔
28

29
        if (Options.Project.FileScopedNamespace)
396✔
30
        {
31
            CodeBuilder.AppendLine(";");
393✔
32
            CodeBuilder.AppendLine();
393✔
33
            GenerateClass();
393✔
34
        }
35
        else
36
        {
37
            CodeBuilder.AppendLine();
3✔
38
            CodeBuilder.AppendLine("{");
3✔
39

40
            using (CodeBuilder.Indent())
3✔
41
            {
42
                GenerateClass();
3✔
43
            }
3✔
44

45
            CodeBuilder.AppendLine("}");
3✔
46
        }
47

48
        return CodeBuilder.ToString();
396✔
49
    }
50

51
    private void GenerateClass()
52
    {
53
        var modelClass = _model.ModelClass.ToSafeName();
396✔
54

55

56
        if (ShouldDocument())
396✔
57
        {
58
            GenerateClassDocumentation();
394✔
59
        }
60
        if (_model.ModelAttributes.HasValue())
396!
61
        {
62
            CodeBuilder.AppendLine(_model.ModelAttributes);
×
63
        }
64
        CodeBuilder.AppendLine($"public partial class {modelClass}");
396✔
65

66
        if (_model.ModelBaseClass.HasValue())
396!
67
        {
68
            var modelBase = _model.ModelBaseClass.ToSafeName();
×
69
            using (CodeBuilder.Indent())
×
70
                CodeBuilder.AppendLine($": {modelBase}");
×
71
        }
72

73
        CodeBuilder.AppendLine("{");
396✔
74

75
        using (CodeBuilder.Indent())
396✔
76
        {
77
            GenerateProperties();
396✔
78
        }
396✔
79

80
        CodeBuilder.AppendLine("}");
396✔
81

82
    }
396✔
83

84

85
    private void GenerateProperties()
86
    {
87
        CodeBuilder.AppendLine("#region Generated Properties");
396✔
88
        foreach (var property in _model.Properties)
6,814✔
89
        {
90
            var propertyType = GetPropertyType(property);
3,011✔
91
            var propertyName = property.PropertyName.ToSafeName();
3,011✔
92

93
            if (ShouldDocument())
3,011✔
94
            {
95
                GeneratePropertyDocumentation(property);
3,006✔
96
            }
97

98
            if (_model.PropertyAttributes.TryGetValue(property.PropertyName, out var attributes) && attributes.Count > 0)
3,011✔
99
            {
100
                foreach (var attribute in attributes)
4✔
101
                    CodeBuilder.AppendLine(attribute);
1✔
102
            }
103

104
            if (property.IsNullable == true && (property.SystemType.IsValueType || Options.Project.Nullable))
3,011✔
105
                CodeBuilder.AppendLine($"public {ToNullablePropertyType(propertyType)} {propertyName} {{ get; set; }}");
1,422✔
106
            else if (Options.Project.Nullable && !property.SystemType.IsValueType)
1,589✔
107
                CodeBuilder.AppendLine($"public {propertyType} {propertyName} {{ get; set; }} = null!;");
438✔
108
            else
109
                CodeBuilder.AppendLine($"public {propertyType} {propertyName} {{ get; set; }}");
1,151✔
110

111
            CodeBuilder.AppendLine();
3,011✔
112
        }
113
        CodeBuilder.AppendLine("#endregion");
396✔
114
        CodeBuilder.AppendLine();
396✔
115
    }
396✔
116

117
    private void GenerateClassDocumentation()
118
    {
119
        var modelType = _model.ModelType switch
394✔
120
        {
394✔
121
            ModelType.Create => "create",
128✔
122
            ModelType.Update => "update",
128✔
123
            _ => "read"
138✔
124
        };
394✔
125

126
        var entityName = ToXmlText(_model.Entity?.EntityClass ?? _model.ModelClass);
394!
127
        var sourceName = ToXmlText(_model.Entity?.TableName);
394!
128
        var sourceType = _model.Entity?.IsView == true ? "view" : "table";
394!
129

130
        CodeBuilder.AppendLine("/// <summary>");
394✔
131

132
        if (sourceName.HasValue())
394!
133
            CodeBuilder.AppendLine($"/// Represents a {modelType} model for the <c>{entityName}</c> entity mapped to the <c>{sourceName}</c> {sourceType}.");
394✔
134
        else
UNCOV
135
            CodeBuilder.AppendLine($"/// Represents a {modelType} model for the <c>{entityName}</c> entity.");
×
136

137
        CodeBuilder.AppendLine("/// </summary>");
394✔
138
    }
394✔
139

140
    private void GeneratePropertyDocumentation(Property property)
141
    {
142
        var propertyName = ToXmlText(property.PropertyName);
3,006✔
143
        var columnName = ToXmlText(property.ColumnName);
3,006✔
144

145
        CodeBuilder.AppendLine("/// <summary>");
3,006✔
146

147
        if (columnName.HasValue())
3,006!
148
            CodeBuilder.AppendLine($"/// Gets or sets the <c>{propertyName}</c> value mapped from the <c>{columnName}</c> column.");
3,006✔
149
        else
UNCOV
150
            CodeBuilder.AppendLine($"/// Gets or sets the <c>{propertyName}</c> value.");
×
151

152
        CodeBuilder.AppendLine("/// </summary>");
3,006✔
153
        CodeBuilder.AppendLine("/// <value>");
3,006✔
154
        CodeBuilder.AppendLine($"/// The <c>{propertyName}</c> model value.");
3,006✔
155
        CodeBuilder.AppendLine("/// </value>");
3,006✔
156
    }
3,006✔
157

158
    private static string GetPropertyType(Property property)
159
    {
160
        return property.SystemTypeName.HasValue()
3,011✔
161
            ? property.SystemTypeName
3,011✔
162
            : property.SystemType.ToType();
3,011✔
163
    }
164

165
    private static string ToNullablePropertyType(string propertyType)
166
    {
167
        return propertyType.EndsWith('?')
1,422!
168
            ? propertyType
1,422✔
169
            : propertyType + "?";
1,422✔
170
    }
171

172

173
    private bool ShouldDocument()
174
    {
175
        if (_model.ModelType == ModelType.Create)
3,407✔
176
            return Options.Model.Create.Document;
1,102✔
177

178
        if (_model.ModelType == ModelType.Update)
2,305✔
179
            return Options.Model.Update.Document;
1,102✔
180

181
        return Options.Model.Read.Document;
1,203✔
182
    }
183
}
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

© 2026 Coveralls, Inc