Skip to content

Commit

Permalink
Make generated code play well with StyleCop
Browse files Browse the repository at this point in the history
Fixes #7189
  • Loading branch information
stefannikolei authored and bricelam committed Apr 5, 2017
1 parent f868cdc commit 67548dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override string GenerateMigration(
"Microsoft.EntityFrameworkCore.Migrations"
};
namespaces.AddRange(GetNamespaces(upOperations.Concat(downOperations)));
foreach (var n in namespaces.Distinct())
foreach (var n in namespaces.OrderBy(x => x).Distinct())
{
builder
.Append("using ")
Expand Down Expand Up @@ -99,6 +99,11 @@ public override string GenerateMigration(
return builder.ToString();
}

private void AppendAutoGeneratedTag(IndentedStringBuilder builder)
{
builder.AppendLine(@"// <auto-generated />");
}

public override string GenerateMetadata(
string migrationNamespace,
Type contextType,
Expand All @@ -113,6 +118,7 @@ public override string GenerateMetadata(
Check.NotNull(targetModel, nameof(targetModel));

var builder = new IndentedStringBuilder();
AppendAutoGeneratedTag(builder);
var namespaces = new List<string>
{
"System",
Expand All @@ -123,7 +129,7 @@ public override string GenerateMetadata(
contextType.Namespace
};
namespaces.AddRange(GetNamespaces(targetModel));
foreach (var n in namespaces.Distinct())
foreach (var n in namespaces.OrderBy(x => x).Distinct())
{
builder
.Append("using ")
Expand Down Expand Up @@ -172,6 +178,7 @@ public override string GenerateSnapshot(
Check.NotNull(model, nameof(model));

var builder = new IndentedStringBuilder();
AppendAutoGeneratedTag(builder);
var namespaces = new List<string>
{
"System",
Expand All @@ -182,7 +189,7 @@ public override string GenerateSnapshot(
contextType.Namespace
};
namespaces.AddRange(GetNamespaces(model));
foreach (var n in namespaces.Distinct())
foreach (var n in namespaces.OrderBy(x => x).Distinct())
{
builder
.Append("using ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public void Migrations_compile()
},
new MigrationOperation[0]);
Assert.Equal(
@"using System;
using System.Collections.Generic;
@"using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace MyNamespace
Expand Down Expand Up @@ -105,12 +105,13 @@ protected override void Down(MigrationBuilder migrationBuilder)
"20150511161616_MyMigration",
new Model { ["Some:EnumValue"] = RegexOptions.Multiline });
Assert.Equal(
@"using System;
@"// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using System;
using System.Text.RegularExpressions;
namespace MyNamespace
Expand Down Expand Up @@ -183,12 +184,13 @@ public void Snapshots_compile()
typeof(MyContext),
"MySnapshot",
model);
Assert.Equal(@"using System;
Assert.Equal(@"// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Design.Tests.Migrations.Design;
using System;
using System.Text;
using System.Text.RegularExpressions;
Expand Down

0 comments on commit 67548dc

Please sign in to comment.