From f021aa03bfbdeb7e254f3df3b879cdb4a94dfe76 Mon Sep 17 00:00:00 2001 From: Simon Reynolds Date: Wed, 31 Mar 2021 16:40:03 +0100 Subject: [PATCH] Always include System namespace in generated Migrations --- .../Design/FSharpMigrationsGenerator.fs | 48 ++++++++----------- .../Design/FSharpMigrationsGeneratorTest.fs | 1 + 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/EFCore.FSharp/Migrations/Design/FSharpMigrationsGenerator.fs b/src/EFCore.FSharp/Migrations/Design/FSharpMigrationsGenerator.fs index 3e3bfc5..686dea2 100644 --- a/src/EFCore.FSharp/Migrations/Design/FSharpMigrationsGenerator.fs +++ b/src/EFCore.FSharp/Migrations/Design/FSharpMigrationsGenerator.fs @@ -33,24 +33,30 @@ type FSharpMigrationsGenerator(dependencies, fSharpDependencies : FSharpMigratio let mutable tempDownOperations = list.Empty let mutable tempMigrationName = String.Empty + let getDefaultNamespaces (contextType: Type) = + seq { + "System" + "Microsoft.EntityFrameworkCore" + "Microsoft.EntityFrameworkCore.Infrastructure" + "Microsoft.EntityFrameworkCore.Metadata" + "Microsoft.EntityFrameworkCore.Migrations" + "Microsoft.EntityFrameworkCore.Storage.ValueConversion" + + if contextType.Namespace |> String.IsNullOrEmpty |> not then + contextType.Namespace + } |> Seq.toList + member private this.GenerateMigrationImpl (migrationNamespace) (migrationName) (migrationId: string) (contextType:Type) (upOperations) (downOperations) (model) = let sb = IndentedStringBuilder() let allOperations = (upOperations |> Seq.append downOperations) - let operationNamespaces = this.GetNamespaces allOperations - + let operationNamespaces = this.GetNamespaces allOperations |> Seq.toList + let namespaces = - [ "Microsoft.EntityFrameworkCore" - "Microsoft.EntityFrameworkCore.Infrastructure" - "Microsoft.EntityFrameworkCore.Metadata" - "Microsoft.EntityFrameworkCore.Migrations" - "Microsoft.EntityFrameworkCore.Storage.ValueConversion" ] - |> Seq.append operationNamespaces - |> Seq.append [contextType.Namespace] - |> Seq.filter (isNull >> not) - |> Seq.toList + (getDefaultNamespaces contextType @ operationNamespaces) |> sortNamespaces + |> Seq.distinct sb |> appendAutoGeneratedTag @@ -99,26 +105,10 @@ type FSharpMigrationsGenerator(dependencies, fSharpDependencies : FSharpMigratio member private this.GenerateSnapshotImpl (modelSnapshotNamespace: string) (contextType: Type) (modelSnapshotName: string) (model: IModel) = let sb = IndentedStringBuilder() - let defaultNamespaces = - seq { - "System" - "Microsoft.EntityFrameworkCore" - "Microsoft.EntityFrameworkCore.Infrastructure" - "Microsoft.EntityFrameworkCore.Metadata" - "Microsoft.EntityFrameworkCore.Migrations" - "Microsoft.EntityFrameworkCore.Storage.ValueConversion" - - if contextType.Namespace |> String.IsNullOrEmpty |> not then - contextType.Namespace - } - |> Seq.toList - - let modelNamespaces = - this.GetNamespaces model - |> Seq.toList + let modelNamespaces = this.GetNamespaces model |> Seq.toList let namespaces = - (defaultNamespaces @ modelNamespaces) + (getDefaultNamespaces contextType @ modelNamespaces) |> sortNamespaces |> Seq.distinct diff --git a/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs b/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs index b931b01..799d85e 100644 --- a/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs +++ b/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs @@ -621,6 +621,7 @@ module FSharpMigrationsGeneratorTest = let expectedCode = """// namespace MyNamespace +open System open System.Text.RegularExpressions open EntityFrameworkCore.FSharp.Test.Migrations.Design open Microsoft.EntityFrameworkCore