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

loresoft / EntityFrameworkCore.Generator / 27760980405

18 Jun 2026 12:56PM UTC coverage: 75.418% (+0.7%) from 74.693%
27760980405

push

github

pwelter34
fix build

1007 of 1723 branches covered (58.44%)

Branch coverage included in aggregate %.

4221 of 5209 relevant lines covered (81.03%)

1242.99 hits per line

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

87.77
/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)
395✔
12
    {
13
        _model = model;
395✔
14
    }
395✔
15

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

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

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

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

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

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

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

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

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

55

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

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

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

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

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

82
    }
395✔
83

84

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

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

98
            if (property.IsNullable == true && (property.SystemType.IsValueType || Options.Project.Nullable))
3,009✔
99
                CodeBuilder.AppendLine($"public {ToNullablePropertyType(propertyType)} {propertyName} {{ get; set; }}");
1,422✔
100
            else if (Options.Project.Nullable && !property.SystemType.IsValueType)
1,587✔
101
                CodeBuilder.AppendLine($"public {propertyType} {propertyName} {{ get; set; }} = null!;");
438✔
102
            else
103
                CodeBuilder.AppendLine($"public {propertyType} {propertyName} {{ get; set; }}");
1,149✔
104

105
            CodeBuilder.AppendLine();
3,009✔
106
        }
107
        CodeBuilder.AppendLine("#endregion");
395✔
108
        CodeBuilder.AppendLine();
395✔
109
    }
395✔
110

111
    private void GenerateClassDocumentation()
112
    {
113
        var modelType = _model.ModelType switch
394✔
114
        {
394✔
115
            ModelType.Create => "create",
128✔
116
            ModelType.Update => "update",
128✔
117
            _ => "read"
138✔
118
        };
394✔
119

120
        var entityName = ToXmlText(_model.Entity?.EntityClass ?? _model.ModelClass);
394!
121
        var sourceName = ToXmlText(_model.Entity?.TableName);
394!
122
        var sourceType = _model.Entity?.IsView == true ? "view" : "table";
394!
123

124
        CodeBuilder.AppendLine("/// <summary>");
394✔
125

126
        if (sourceName.HasValue())
394!
127
            CodeBuilder.AppendLine($"/// Represents a {modelType} model for the <c>{entityName}</c> entity mapped to the <c>{sourceName}</c> {sourceType}.");
394✔
128
        else
129
            CodeBuilder.AppendLine($"/// Represents a {modelType} model for the <c>{entityName}</c> entity.");
×
130

131
        CodeBuilder.AppendLine("/// </summary>");
394✔
132
    }
394✔
133

134
    private void GeneratePropertyDocumentation(Property property)
135
    {
136
        var propertyName = ToXmlText(property.PropertyName);
3,006✔
137
        var columnName = ToXmlText(property.ColumnName);
3,006✔
138

139
        CodeBuilder.AppendLine("/// <summary>");
3,006✔
140

141
        if (columnName.HasValue())
3,006!
142
            CodeBuilder.AppendLine($"/// Gets or sets the <c>{propertyName}</c> value mapped from the <c>{columnName}</c> column.");
3,006✔
143
        else
144
            CodeBuilder.AppendLine($"/// Gets or sets the <c>{propertyName}</c> value.");
×
145

146
        CodeBuilder.AppendLine("/// </summary>");
3,006✔
147
        CodeBuilder.AppendLine("/// <value>");
3,006✔
148
        CodeBuilder.AppendLine($"/// The <c>{propertyName}</c> model value.");
3,006✔
149
        CodeBuilder.AppendLine("/// </value>");
3,006✔
150
    }
3,006✔
151

152
    private static string GetPropertyType(Property property)
153
    {
154
        return property.SystemTypeName.HasValue()
3,009✔
155
            ? property.SystemTypeName
3,009✔
156
            : property.SystemType.ToType();
3,009✔
157
    }
158

159
    private static string ToNullablePropertyType(string propertyType)
160
    {
161
        return propertyType.EndsWith('?')
1,422!
162
            ? propertyType
1,422✔
163
            : propertyType + "?";
1,422✔
164
    }
165

166

167
    private bool ShouldDocument()
168
    {
169
        if (_model.ModelType == ModelType.Create)
3,404✔
170
            return Options.Model.Create.Document;
1,102✔
171

172
        if (_model.ModelType == ModelType.Update)
2,302✔
173
            return Options.Model.Update.Document;
1,102✔
174

175
        return Options.Model.Read.Document;
1,200✔
176
    }
177
}
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