Skip to content

Commit fe85801

Browse files
Generate file-scoped namespaces and omit BOM in generated C# (#38256)
Fixes #14545 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent e3b2d36 commit fe85801

329 files changed

Lines changed: 165217 additions & 165612 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ public override string GenerateMigration(
8686
{
8787
builder
8888
.AppendLine()
89-
.Append("namespace ").AppendLine(Code.Namespace(migrationNamespace))
90-
.AppendLine("{")
91-
.IncrementIndent();
89+
.Append("namespace ").Append(Code.Namespace(migrationNamespace)).AppendLine(";");
9290
}
9391

9492
builder
93+
.AppendLine()
9594
.AppendLine("/// <inheritdoc />")
9695
.Append("public partial class ").Append(Code.Identifier(migrationName)).AppendLine(" : Migration")
9796
.AppendLine("{");
@@ -125,13 +124,6 @@ public override string GenerateMigration(
125124

126125
builder.AppendLine("}");
127126

128-
if (!string.IsNullOrEmpty(migrationNamespace))
129-
{
130-
builder
131-
.DecrementIndent()
132-
.AppendLine("}");
133-
}
134-
135127
return builder.ToString();
136128
}
137129

@@ -185,12 +177,11 @@ public override string GenerateMetadata(
185177
{
186178
builder
187179
.AppendLine()
188-
.Append("namespace ").AppendLine(Code.Namespace(migrationNamespace))
189-
.AppendLine("{")
190-
.IncrementIndent();
180+
.Append("namespace ").Append(Code.Namespace(migrationNamespace)).AppendLine(";");
191181
}
192182

193183
builder
184+
.AppendLine()
194185
.Append("[DbContext(typeof(").Append(Code.Reference(contextType)).AppendLine("))]")
195186
.Append("[Migration(").Append(Code.Literal(migrationId)).AppendLine(")]")
196187
.Append("partial class ").AppendLine(Code.Identifier(migrationName))
@@ -202,9 +193,7 @@ public override string GenerateMetadata(
202193
.AppendLine("protected override void BuildTargetModel(ModelBuilder modelBuilder)")
203194
.AppendLine("{")
204195
.DecrementIndent()
205-
.DecrementIndent()
206196
.AppendLine("#pragma warning disable 612, 618")
207-
.IncrementIndent()
208197
.IncrementIndent();
209198
using (builder.Indent())
210199
{
@@ -213,23 +202,14 @@ public override string GenerateMetadata(
213202
}
214203

215204
builder
216-
.DecrementIndent()
217205
.DecrementIndent()
218206
.AppendLine("#pragma warning restore 612, 618")
219207
.IncrementIndent()
220-
.IncrementIndent()
221208
.AppendLine("}");
222209
}
223210

224211
builder.AppendLine("}");
225212

226-
if (!string.IsNullOrEmpty(migrationNamespace))
227-
{
228-
builder
229-
.DecrementIndent()
230-
.AppendLine("}");
231-
}
232-
233213
return builder.ToString();
234214
}
235215

@@ -279,12 +259,11 @@ public override string GenerateSnapshot(
279259
{
280260
builder
281261
.AppendLine()
282-
.Append("namespace ").AppendLine(Code.Namespace(modelSnapshotNamespace))
283-
.AppendLine("{")
284-
.IncrementIndent();
262+
.Append("namespace ").Append(Code.Namespace(modelSnapshotNamespace)).AppendLine(";");
285263
}
286264

287265
builder
266+
.AppendLine()
288267
.Append("[DbContext(typeof(").Append(Code.Reference(contextType)).AppendLine("))]")
289268
.Append("partial class ").Append(Code.Identifier(modelSnapshotName)).AppendLine(" : ModelSnapshot")
290269
.AppendLine("{");
@@ -304,33 +283,22 @@ public override string GenerateSnapshot(
304283
.AppendLine("protected override void BuildModel(ModelBuilder modelBuilder)")
305284
.AppendLine("{")
306285
.DecrementIndent()
307-
.DecrementIndent()
308286
.AppendLine("#pragma warning disable 612, 618")
309-
.IncrementIndent()
310287
.IncrementIndent();
311288
using (builder.Indent())
312289
{
313290
CSharpDependencies.CSharpSnapshotGenerator.Generate("modelBuilder", model, builder);
314291
}
315292

316293
builder
317-
.DecrementIndent()
318294
.DecrementIndent()
319295
.AppendLine("#pragma warning restore 612, 618")
320296
.IncrementIndent()
321-
.IncrementIndent()
322297
.AppendLine("}");
323298
}
324299

325300
builder.AppendLine("}");
326301

327-
if (!string.IsNullOrEmpty(modelSnapshotNamespace))
328-
{
329-
builder
330-
.DecrementIndent()
331-
.AppendLine("}");
332-
}
333-
334302
return builder.ToString();
335303
}
336304

src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public virtual MigrationFiles RemoveMigration(
392392

393393
if (!dryRun)
394394
{
395-
File.WriteAllText(modelSnapshotFile, modelSnapshotCode, Encoding.UTF8);
395+
File.WriteAllText(modelSnapshotFile, modelSnapshotCode, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
396396
}
397397
}
398398

@@ -424,11 +424,11 @@ public virtual MigrationFiles Save(string projectDir, ScaffoldedMigration migrat
424424
Directory.CreateDirectory(migrationDirectory);
425425
Directory.CreateDirectory(modelSnapshotDirectory);
426426

427-
File.WriteAllText(migrationFile, migration.MigrationCode, Encoding.UTF8);
428-
File.WriteAllText(migrationMetadataFile, migration.MetadataCode, Encoding.UTF8);
427+
File.WriteAllText(migrationFile, migration.MigrationCode, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
428+
File.WriteAllText(migrationMetadataFile, migration.MetadataCode, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
429429

430430
Dependencies.OperationReporter.WriteVerbose(DesignStrings.WritingSnapshot(modelSnapshotFile));
431-
File.WriteAllText(modelSnapshotFile, migration.SnapshotCode, Encoding.UTF8);
431+
File.WriteAllText(modelSnapshotFile, migration.SnapshotCode, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
432432
}
433433

434434
return new MigrationFiles

src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,8 @@ private string GenerateUnsafeAccessorType(
216216
if (!string.IsNullOrEmpty(@namespace))
217217
{
218218
mainBuilder
219-
.Append("namespace ").AppendLine(_code.Namespace(@namespace))
220-
.AppendLine("{");
221-
mainBuilder.Indent();
219+
.Append("namespace ").Append(_code.Namespace(@namespace)).AppendLine(";")
220+
.AppendLine();
222221
}
223222

224223
mainBuilder
@@ -330,12 +329,6 @@ private string GenerateUnsafeAccessorType(
330329

331330
mainBuilder.AppendLine("}");
332331

333-
if (!string.IsNullOrEmpty(@namespace))
334-
{
335-
mainBuilder.DecrementIndent();
336-
mainBuilder.AppendLine("}");
337-
}
338-
339332
return GenerateHeader(namespaces, @namespace, nullable) + mainBuilder;
340333
}
341334

@@ -355,9 +348,8 @@ private string CreateModel(
355348
if (!string.IsNullOrEmpty(@namespace))
356349
{
357350
mainBuilder
358-
.Append("namespace ").AppendLine(_code.Namespace(@namespace))
359-
.AppendLine("{");
360-
mainBuilder.Indent();
351+
.Append("namespace ").Append(_code.Namespace(@namespace)).AppendLine(";")
352+
.AppendLine();
361353
}
362354

363355
var className = GetModelClassName(contextType);
@@ -413,12 +405,6 @@ void RunInitialization()
413405

414406
mainBuilder.AppendLine("}");
415407

416-
if (!string.IsNullOrEmpty(@namespace))
417-
{
418-
mainBuilder.DecrementIndent();
419-
mainBuilder.AppendLine("}");
420-
}
421-
422408
return GenerateHeader(namespaces, @namespace, nullable) + mainBuilder;
423409
}
424410

@@ -440,9 +426,8 @@ private string CreateModelBuilder(
440426
if (!string.IsNullOrEmpty(@namespace))
441427
{
442428
mainBuilder
443-
.Append("namespace ").AppendLine(_code.Namespace(@namespace))
444-
.AppendLine("{");
445-
mainBuilder.Indent();
429+
.Append("namespace ").Append(_code.Namespace(@namespace)).AppendLine(";")
430+
.AppendLine();
446431
}
447432

448433
var className = GetModelClassName(contextType);
@@ -625,12 +610,6 @@ private string CreateModelBuilder(
625610

626611
mainBuilder.AppendLine("}");
627612

628-
if (!string.IsNullOrEmpty(@namespace))
629-
{
630-
mainBuilder.DecrementIndent();
631-
mainBuilder.AppendLine("}");
632-
}
633-
634613
return GenerateHeader(namespaces, @namespace, nullable) + mainBuilder;
635614
}
636615

@@ -727,9 +706,8 @@ private string GenerateEntityType(
727706
if (!string.IsNullOrEmpty(@namespace))
728707
{
729708
mainBuilder
730-
.Append("namespace ").AppendLine(_code.Namespace(@namespace))
731-
.AppendLine("{");
732-
mainBuilder.Indent();
709+
.Append("namespace ").Append(_code.Namespace(@namespace)).AppendLine(";")
710+
.AppendLine();
733711
}
734712

735713
AddNamespace(typeof(EntityFrameworkInternalAttribute), namespaces);
@@ -780,12 +758,6 @@ private string GenerateEntityType(
780758

781759
mainBuilder.AppendLine("}");
782760

783-
if (!string.IsNullOrEmpty(@namespace))
784-
{
785-
mainBuilder.DecrementIndent();
786-
mainBuilder.AppendLine("}");
787-
}
788-
789761
return GenerateHeader(namespaces, @namespace, nullable) + mainBuilder;
790762
}
791763

src/EFCore.Design/Scaffolding/Internal/CompiledModelScaffolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static IReadOnlyList<string> WriteFiles(
8181
}
8282
else
8383
{
84-
File.WriteAllText(fullPath, file.Code, Encoding.UTF8);
84+
File.WriteAllText(fullPath, file.Code, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
8585
savedFiles.Add(fullPath);
8686
}
8787
}

src/EFCore.Design/Scaffolding/Internal/ReverseEngineerScaffolder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ public virtual SavedModelFiles Save(
135135

136136
var contextPath = Path.GetFullPath(Path.Combine(outputDir, scaffoldedModel.ContextFile.Path));
137137
Directory.CreateDirectory(Path.GetDirectoryName(contextPath)!);
138-
File.WriteAllText(contextPath, scaffoldedModel.ContextFile.Code, Encoding.UTF8);
138+
File.WriteAllText(contextPath, scaffoldedModel.ContextFile.Code, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
139139

140140
var additionalFiles = new List<string>();
141141
foreach (var entityTypeFile in scaffoldedModel.AdditionalFiles)
142142
{
143143
var additionalFilePath = Path.Combine(outputDir, entityTypeFile.Path);
144-
File.WriteAllText(additionalFilePath, entityTypeFile.Code, Encoding.UTF8);
144+
File.WriteAllText(additionalFilePath, entityTypeFile.Code, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
145145
additionalFiles.Add(additionalFilePath);
146146
}
147147

0 commit comments

Comments
 (0)