-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
DatabaseOperations.cs
177 lines (156 loc) · 7.49 KB
/
DatabaseOperations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.EntityFrameworkCore.Design.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class DatabaseOperations
{
private readonly IOperationReporter _reporter;
private readonly string _projectDir;
private readonly string? _rootNamespace;
private readonly string? _language;
private readonly bool _nullable;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly string[] _args;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public DatabaseOperations(
IOperationReporter reporter,
Assembly assembly,
Assembly startupAssembly,
string projectDir,
string? rootNamespace,
string? language,
bool nullable,
string[]? args)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));
_reporter = reporter;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_nullable = nullable;
_args = args ?? Array.Empty<string>();
_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual SavedModelFiles ScaffoldContext(
string provider,
string connectionString,
string? outputDir,
string? outputContextDir,
string? dbContextClassName,
IEnumerable<string> schemas,
IEnumerable<string> tables,
string? modelNamespace,
string? contextNamespace,
bool useDataAnnotations,
bool overwriteFiles,
bool useDatabaseNames,
bool suppressOnConfiguring,
bool noPluralize)
{
Check.NotEmpty(provider, nameof(provider));
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(schemas, nameof(schemas));
Check.NotNull(tables, nameof(tables));
outputDir = outputDir != null
? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
: _projectDir;
outputContextDir = outputContextDir != null
? Path.GetFullPath(Path.Combine(_projectDir, outputContextDir))
: outputDir;
var services = _servicesBuilder.Build(provider);
var scaffolder = services.GetRequiredService<IReverseEngineerScaffolder>();
var finalModelNamespace = modelNamespace ?? GetNamespaceFromOutputPath(outputDir);
var finalContextNamespace =
contextNamespace ?? modelNamespace ?? GetNamespaceFromOutputPath(outputContextDir);
var scaffoldedModel = scaffolder.ScaffoldModel(
connectionString,
new DatabaseModelFactoryOptions(tables, schemas),
new ModelReverseEngineerOptions { UseDatabaseNames = useDatabaseNames, NoPluralize = noPluralize },
new ModelCodeGenerationOptions
{
UseDataAnnotations = useDataAnnotations,
RootNamespace = _rootNamespace,
ModelNamespace = finalModelNamespace,
ContextNamespace = finalContextNamespace,
Language = _language,
UseNullableReferenceTypes = _nullable,
ContextDir = MakeDirRelative(outputDir, outputContextDir),
ContextName = dbContextClassName,
SuppressOnConfiguring = suppressOnConfiguring
});
return scaffolder.Save(
scaffoldedModel,
outputDir,
overwriteFiles);
}
private string? GetNamespaceFromOutputPath(string directoryPath)
{
var subNamespace = SubnamespaceFromOutputPath(_projectDir, directoryPath);
return string.IsNullOrEmpty(subNamespace)
? _rootNamespace
: string.IsNullOrEmpty(_rootNamespace)
? subNamespace
: _rootNamespace + "." + subNamespace;
}
// if outputDir is a subfolder of projectDir, then use each subfolder as a subnamespace
// --output-dir $(projectFolder)/A/B/C
// => "namespace $(rootnamespace).A.B.C"
private static string? SubnamespaceFromOutputPath(string projectDir, string outputDir)
{
if (!outputDir.StartsWith(projectDir, StringComparison.Ordinal))
{
return null;
}
var subPath = outputDir.Substring(projectDir.Length);
return !string.IsNullOrWhiteSpace(subPath)
? string.Join(
".",
subPath.Split(
new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries))
: null;
}
private static string MakeDirRelative(string root, string path)
{
var relativeUri = new Uri(NormalizeDir(root)).MakeRelativeUri(new Uri(NormalizeDir(path)));
return Uri.UnescapeDataString(relativeUri.ToString()).Replace('/', Path.DirectorySeparatorChar);
}
private static string NormalizeDir(string path)
{
if (string.IsNullOrEmpty(path))
{
return path;
}
var last = path[path.Length - 1];
return last == Path.DirectorySeparatorChar
|| last == Path.AltDirectorySeparatorChar
? path
: path + Path.DirectorySeparatorChar;
}
}
}