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

loresoft / EntityFrameworkCore.Generator / 27827701138

19 Jun 2026 01:10PM UTC coverage: 71.692% (-0.07%) from 71.762%
27827701138

push

github

pwelter34
Handle exceptions in commands

985 of 1863 branches covered (52.87%)

Branch coverage included in aggregate %.

14 of 52 new or added lines in 3 files covered. (26.92%)

1 existing line in 1 file now uncovered.

4217 of 5393 relevant lines covered (78.19%)

1276.45 hits per line

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

0.0
/src/EntityFrameworkCore.Generator/InitializeCommand.cs
1
using EntityFrameworkCore.Generator.Extensions;
2
using EntityFrameworkCore.Generator.Options;
3

4
using Microsoft.Extensions.Logging;
5

6
using Spectre.Console.Cli;
7

8
namespace EntityFrameworkCore.Generator;
9

10
public class InitializeCommand : AsyncCommand<InitializeSettings>
11
{
12
    private readonly ILogger<InitializeCommand> _logger;
13
    private readonly IConfigurationSerializer _serializer;
14

15
    public InitializeCommand(ILogger<InitializeCommand> logger, IConfigurationSerializer serializer)
×
16
    {
17
        _logger = logger;
×
18
        _serializer = serializer;
×
19
    }
×
20

21
    protected override Task<int> ExecuteAsync(CommandContext context, InitializeSettings settings, CancellationToken cancellationToken)
22
    {
23
        try
24
        {
NEW
25
            var workingDirectory = settings.WorkingDirectory ?? Environment.CurrentDirectory;
×
26

NEW
27
            if (!Directory.Exists(workingDirectory))
×
28
            {
NEW
29
                _logger.LogTrace("Creating directory: {Directory}", workingDirectory);
×
NEW
30
                Directory.CreateDirectory(workingDirectory);
×
31
            }
32

NEW
33
            var optionsFile = settings.OptionsFile ?? ConfigurationSerializer.OptionsFileName;
×
34

NEW
35
            Serialization.GeneratorModel? options = null;
×
36

NEW
37
            if (_serializer.Exists(workingDirectory, optionsFile))
×
NEW
38
                options = _serializer.Load(workingDirectory, optionsFile);
×
39

NEW
40
            if (options == null)
×
NEW
41
                options = CreateOptionsFile(optionsFile);
×
42

NEW
43
            if (settings.UserSecretsId.HasValue())
×
NEW
44
                options.Database.UserSecretsId = settings.UserSecretsId;
×
45

NEW
46
            if (settings.ConnectionName.HasValue())
×
NEW
47
                options.Database.ConnectionName = settings.ConnectionName;
×
48

NEW
49
            if (settings.Provider.HasValue)
×
NEW
50
                options.Database.Provider = settings.Provider.Value;
×
51

NEW
52
            if (settings.ConnectionString.HasValue())
×
53
            {
NEW
54
                if (settings.UserSecretsId.HasValue())
×
NEW
55
                    options = CreateUserSecret(options, settings.ConnectionString);
×
56
                else
NEW
57
                    options.Database.ConnectionString = settings.ConnectionString;
×
58
            }
59

NEW
60
            _serializer.Save(options, workingDirectory, optionsFile);
×
61

NEW
62
            return Task.FromResult(0);
×
63
        }
NEW
64
        catch (Exception ex)
×
65
        {
NEW
66
            _logger.LogError(ex, ex.Message);
×
NEW
67
            return Task.FromResult(1);
×
68
        }
UNCOV
69
    }
×
70

71
    private Serialization.GeneratorModel CreateUserSecret(Serialization.GeneratorModel options, string connectionString)
72
    {
73
        if (options.Database.UserSecretsId.IsNullOrWhiteSpace())
×
74
            options.Database.UserSecretsId = Guid.NewGuid().ToString();
×
75

76
        if (options.Database.ConnectionName.IsNullOrWhiteSpace())
×
77
            options.Database.ConnectionName = "ConnectionStrings:Generator";
×
78

79
        _logger.LogInformation("Adding Connection String to User Secrets file");
×
80

81
        // save connection string to user secrets file
82
        var secretsStore = new SecretsStore(options.Database.UserSecretsId);
×
83
        secretsStore.Set(options.Database.ConnectionName, connectionString);
×
84
        secretsStore.Save();
×
85

86
        return options;
×
87
    }
88

89
    private Serialization.GeneratorModel CreateOptionsFile(string optionsFile)
90
    {
91
        var options = new Serialization.GeneratorModel();
×
92

93
        options.Project.Namespace = Directory.CreateDirectory(Environment.CurrentDirectory).Name ?? "Project.Core";
×
94
        options.Project.Directory = ".\\";
×
95
        options.Project.Nullable = true;
×
96
        options.Project.FileScopedNamespace = true;
×
97

98
        options.Data.Mapping.RowVersion = RowVersionMapping.Long;
×
99

100
        // default all to generate
101
        options.Data.Query.Generate = true;
×
102
        options.Model.Read.Generate = true;
×
103
        options.Model.Create.Generate = true;
×
104
        options.Model.Update.Generate = true;
×
105
        options.Model.Validator.Generate = true;
×
106
        options.Model.Mapper.Generate = true;
×
107

108
        _logger.LogInformation("Creating options file: {OptionsFile}", optionsFile);
×
109

110
        return options;
×
111
    }
112
}
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