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

loresoft / EntityFrameworkCore.Generator / 15303740763

28 May 2025 03:07PM UTC coverage: 54.841% (-0.2%) from 55.036%
15303740763

push

github

pwelter34
add file header support

642 of 1331 branches covered (48.23%)

Branch coverage included in aggregate %.

14 of 33 new or added lines in 14 files covered. (42.42%)

3 existing lines in 3 files now uncovered.

1873 of 3255 relevant lines covered (57.54%)

61.12 hits per line

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

73.39
/src/EntityFrameworkCore.Generator.Core/Templates/DataContextTemplate.cs
1
using System.Linq;
2

3
using EntityFrameworkCore.Generator.Extensions;
4
using EntityFrameworkCore.Generator.Metadata.Generation;
5
using EntityFrameworkCore.Generator.Options;
6

7
namespace EntityFrameworkCore.Generator.Templates;
8

9
public class DataContextTemplate : CodeTemplateBase
10
{
11
    private readonly EntityContext _entityContext;
12

13
    public DataContextTemplate(EntityContext entityContext, GeneratorOptions options) : base(options)
2✔
14
    {
15
        _entityContext = entityContext;
2✔
16
    }
2✔
17

18
    public override string WriteCode()
19
    {
20
        CodeBuilder.Clear();
2✔
21

22
        if (Options.Data.Context.Header.HasValue())
2!
NEW
23
            CodeBuilder.AppendLine(Options.Data.Context.Header).AppendLine();
×
24

25
        CodeBuilder.AppendLine("using System;");
2✔
26
        CodeBuilder.AppendLine();
2✔
27
        CodeBuilder.AppendLine("using Microsoft.EntityFrameworkCore;");
2✔
28
        CodeBuilder.AppendLine("using Microsoft.EntityFrameworkCore.Metadata;");
2✔
29
        CodeBuilder.AppendLine();
2✔
30

31
        CodeBuilder.Append($"namespace {_entityContext.ContextNamespace}");
2✔
32

33
        if (Options.Project.FileScopedNamespace)
2!
34
        {
35
            CodeBuilder.AppendLine(";");
×
36
            CodeBuilder.AppendLine();
×
37
            GenerateClass();
×
38
        }
39
        else
40
        {
41
            CodeBuilder.AppendLine();
2✔
42
            CodeBuilder.AppendLine("{");
2✔
43

44
            using (CodeBuilder.Indent())
2✔
45
            {
46
                GenerateClass();
2✔
47
            }
2✔
48

49
            CodeBuilder.AppendLine("}");
2✔
50
        }
51

52
        return CodeBuilder.ToString();
2✔
53
    }
54

55

56
    private void GenerateClass()
57
    {
58
        var contextClass = _entityContext.ContextClass.ToSafeName();
2✔
59
        var baseClass = _entityContext.ContextBaseClass.ToSafeName();
2✔
60

61
        if (Options.Data.Context.Document)
2!
62
        {
63
            CodeBuilder.AppendLine("/// <summary>");
×
64
            CodeBuilder.AppendLine("/// A <see cref=\"DbContext\" /> instance represents a session with the database and can be used to query and save instances of entities. ");
×
65
            CodeBuilder.AppendLine("/// </summary>");
×
66
        }
67

68
        CodeBuilder.AppendLine($"public partial class {contextClass} : {baseClass}");
2✔
69
        CodeBuilder.AppendLine("{");
2✔
70

71
        using (CodeBuilder.Indent())
2✔
72
        {
73
            GenerateConstructors();
2✔
74
            GenerateDbSets();
2✔
75
            GenerateOnConfiguring();
2✔
76
        }
2✔
77

78
        CodeBuilder.AppendLine("}");
2✔
79
    }
2✔
80

81
    private void GenerateConstructors()
82
    {
83
        var contextName = _entityContext.ContextClass.ToSafeName();
2✔
84

85
        if (Options.Data.Context.Document)
2!
86
        {
87
            CodeBuilder.AppendLine("/// <summary>");
×
88
            CodeBuilder.AppendLine($"/// Initializes a new instance of the <see cref=\"{contextName}\"/> class.");
×
89
            CodeBuilder.AppendLine("/// </summary>");
×
90
            CodeBuilder.AppendLine("/// <param name=\"options\">The options to be used by this <see cref=\"DbContext\" />.</param>");
×
91
        }
92

93
        CodeBuilder.AppendLine($"public {contextName}(DbContextOptions<{contextName}> options)")
2✔
94
            .IncrementIndent()
2✔
95
            .AppendLine(": base(options)")
2✔
96
            .DecrementIndent()
2✔
97
            .AppendLine("{")
2✔
98
            .AppendLine("}")
2✔
99
            .AppendLine();
2✔
100
    }
2✔
101

102
    private void GenerateDbSets()
103
    {
104
        CodeBuilder.AppendLine("#region Generated Properties");
2✔
105
        foreach (var entityType in _entityContext.Entities.OrderBy(e => e.ContextProperty))
70✔
106
        {
107
            var entityClass = entityType.EntityClass.ToSafeName();
22✔
108
            var propertyName = entityType.ContextProperty.ToSafeName();
22✔
109
            var fullName = $"{entityType.EntityNamespace}.{entityClass}";
22✔
110

111
            if (Options.Data.Context.Document)
22!
112
            {
113
                CodeBuilder.AppendLine("/// <summary>");
×
114
                CodeBuilder.AppendLine($"/// Gets or sets the <see cref=\"T:Microsoft.EntityFrameworkCore.DbSet`1\" /> that can be used to query and save instances of <see cref=\"{fullName}\"/>.");
×
115
                CodeBuilder.AppendLine("/// </summary>");
×
116
                CodeBuilder.AppendLine("/// <value>");
×
117
                CodeBuilder.AppendLine($"/// The <see cref=\"T:Microsoft.EntityFrameworkCore.DbSet`1\" /> that can be used to query and save instances of <see cref=\"{fullName}\"/>.");
×
118
                CodeBuilder.AppendLine("/// </value>");
×
119
            }
120

121
            CodeBuilder.Append($"public virtual DbSet<{fullName}> {propertyName} {{ get; set; }}");
22✔
122
            if (Options.Project.Nullable)
22!
123
                CodeBuilder.Append(" = null!;");
×
124

125
            CodeBuilder.AppendLine();
22✔
126
            CodeBuilder.AppendLine();
22✔
127
        }
128

129
        CodeBuilder.AppendLine("#endregion");
2✔
130

131
        if (_entityContext.Entities.Any())
2✔
132
            CodeBuilder.AppendLine();
2✔
133
    }
2✔
134

135
    private void GenerateOnConfiguring()
136
    {
137
        if (Options.Data.Context.Document)
2!
138
        {
139
            CodeBuilder.AppendLine("/// <summary>");
×
140
            CodeBuilder.AppendLine("/// Configure the model that was discovered from the entity types exposed in <see cref=\"T:Microsoft.EntityFrameworkCore.DbSet`1\" /> properties on this context.");
×
141
            CodeBuilder.AppendLine("/// </summary>");
×
142
            CodeBuilder.AppendLine("/// <param name=\"modelBuilder\">The builder being used to construct the model for this context.</param>");
×
143
        }
144

145
        CodeBuilder.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)");
2✔
146
        CodeBuilder.AppendLine("{");
2✔
147

148
        using (CodeBuilder.Indent())
2✔
149
        {
150
            CodeBuilder.AppendLine("#region Generated Configuration");
2✔
151
            foreach (var entityType in _entityContext.Entities.OrderBy(e => e.MappingClass))
70✔
152
            {
153
                var mappingClass = entityType.MappingClass.ToSafeName();
22✔
154

155
                CodeBuilder.AppendLine($"modelBuilder.ApplyConfiguration(new {entityType.MappingNamespace}.{mappingClass}());");
22✔
156
            }
157

158
            CodeBuilder.AppendLine("#endregion");
2✔
159
        }
2✔
160

161
        CodeBuilder.AppendLine("}");
2✔
162
    }
2✔
163
}
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