Skip to content

Commit

Permalink
Generate using statements for namespaces of custom property types
Browse files Browse the repository at this point in the history
Issue #5912
  • Loading branch information
ajcvickers committed Oct 11, 2016
1 parent 62de4bd commit 79f393e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ public abstract string GenerateSnapshot(
[NotNull] IModel model);

protected virtual IEnumerable<string> GetNamespaces([NotNull] IEnumerable<MigrationOperation> operations)
=> GetAnnotationNamespaces(GetAnnotatables(operations));
=> operations.OfType<ColumnOperation>().SelectMany(GetColumnNamespaces)
.Concat(operations.OfType<CreateTableOperation>().SelectMany(o => o.Columns).SelectMany(GetColumnNamespaces))
.Concat(GetAnnotationNamespaces(GetAnnotatables(operations)));

private static IEnumerable<string> GetColumnNamespaces(ColumnOperation columnOperation)
{
yield return columnOperation.ClrType.Namespace;

var alterColumnOperation = columnOperation as AlterColumnOperation;
if (alterColumnOperation?.OldColumn != null)
{
yield return alterColumnOperation.OldColumn.ClrType.Namespace;
}
}

private IEnumerable<IAnnotatable> GetAnnotatables(IEnumerable<MigrationOperation> operations)
{
Expand Down Expand Up @@ -69,8 +82,9 @@ private IEnumerable<IAnnotatable> GetAnnotatables(IEnumerable<MigrationOperation
}

protected virtual IEnumerable<string> GetNamespaces([NotNull] IModel model)
=> GetAnnotationNamespaces(GetAnnotatables(model));

=> model.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties().Select(p => p.ClrType.Namespace))
.Concat(GetAnnotationNamespaces(GetAnnotatables(model)));

private IEnumerable<IAnnotatable> GetAnnotatables(IModel model)
{
yield return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -13,6 +15,7 @@
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Relational.Design.Specification.Tests.TestUtilities;
using Microsoft.EntityFrameworkCore.Storage;
using Xunit;

namespace Microsoft.EntityFrameworkCore.FunctionalTests.Migrations
Expand All @@ -31,19 +34,38 @@ public void Migrations_compile()
var migrationCode = generator.GenerateMigration(
"MyNamespace",
"MyMigration",
new[]
new MigrationOperation[]
{
new SqlOperation
{
Sql = "-- TEST",
["Some:EnumValue"] = RegexOptions.Multiline
},
new AlterColumnOperation
{
Name = "C2",
Table = "T1",
ClrType = typeof(Database),
OldColumn = new ColumnOperation
{
ClrType = typeof(Property)
}
},
new AddColumnOperation
{
Name = "C3",
Table = "T1",
ClrType = typeof(PropertyEntry)
}
},
new MigrationOperation[0]);
Assert.Equal(
@"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Text.RegularExpressions;
namespace MyNamespace
Expand All @@ -54,6 +76,17 @@ protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(""-- TEST"")
.Annotation(""Some:EnumValue"", RegexOptions.Multiline);
migrationBuilder.AlterColumn<Database>(
name: ""C2"",
table: ""T1"",
nullable: false,
oldClrType: typeof(Property));
migrationBuilder.AddColumn<PropertyEntry>(
name: ""C3"",
table: ""T1"",
nullable: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
Expand Down Expand Up @@ -104,7 +137,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder)
BuildReference.ByName("System.Text.RegularExpressions"),
#else
BuildReference.ByName("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
BuildReference.ByName("System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
BuildReference.ByName("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),
#endif
BuildReference.ByName("Microsoft.EntityFrameworkCore.Design.FunctionalTests", depContextAssembly: GetType().GetTypeInfo().Assembly),
BuildReference.ByName("Microsoft.EntityFrameworkCore"),
Expand All @@ -125,7 +158,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder)

Assert.Equal("20150511161616_MyMigration", migration.GetId());

Assert.Equal(1, migration.UpOperations.Count);
Assert.Equal(3, migration.UpOperations.Count);
Assert.Empty(migration.DownOperations);
Assert.Empty(migration.TargetModel.GetEntityTypes());
}
Expand All @@ -139,17 +172,22 @@ public void Snapshots_compile()
new CSharpMigrationOperationGenerator(codeHelper),
new CSharpSnapshotGenerator(codeHelper));

var model = new Model { ["Some:EnumValue"] = RegexOptions.Multiline };
var entityType = model.AddEntityType("Cheese");
entityType.AddProperty("Pickle", typeof(StringBuilder));

var modelSnapshotCode = generator.GenerateSnapshot(
"MyNamespace",
typeof(MyContext),
"MySnapshot",
new Model { ["Some:EnumValue"] = RegexOptions.Multiline });
model);
Assert.Equal(@"using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.FunctionalTests.Migrations;
using System.Text;
using System.Text.RegularExpressions;
namespace MyNamespace
Expand All @@ -161,13 +199,20 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation(""Some:EnumValue"", RegexOptions.Multiline);
modelBuilder.Entity(""Cheese"", b =>
{
b.Property<StringBuilder>(""Pickle"");
b.ToTable(""Cheese"");
});
}
}
}
", modelSnapshotCode);

var snapshot = CompileModelSnapshot(modelSnapshotCode, "MyNamespace.MySnapshot");
Assert.Empty(snapshot.Model.GetEntityTypes());
Assert.Equal(1, snapshot.Model.GetEntityTypes().Count());
}

[Fact]
Expand Down

0 comments on commit 79f393e

Please sign in to comment.