diff --git a/CHANGELOG.md b/CHANGELOG.md index cb51567..3deb940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed +- Fix indentation issue when table has constraints - https://github.com/efcore/EFCore.FSharp/pull/75 + ## [5.0.3-alpha5] - 2021-03-31 ### Fixed diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 5926c1a..28c807f 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -120,8 +120,7 @@ For this example we will use record types, but "normal" classes will also work i type BloggingContext() = inherit DbContext() - [] val mutable blogs : DbSet - member __.Blogs with get() = __.blogs and set v = __.blogs <- v + member val Blogs : DbSet = null with get, set override __.OnConfiguring(options: DbContextOptionsBuilder) : unit = options.UseSqlite("Data Source=blogging.db") |> ignore diff --git a/docsSrc/How_Tos/Use_DbContextHelpers.md b/docsSrc/How_Tos/Use_DbContextHelpers.md index 8b200b2..3e93a46 100644 --- a/docsSrc/How_Tos/Use_DbContextHelpers.md +++ b/docsSrc/How_Tos/Use_DbContextHelpers.md @@ -27,9 +27,7 @@ type Blog = { type MyContext () = inherit DbContext() - [] - val mutable private _blogs : DbSet - member this.Blogs with get() = this._blogs and set v = this._blogs <- v + member val Blogs : DbSet = null with get, set ``` We can use the helper methods like so diff --git a/src/EFCore.FSharp/Migrations/Design/FSharpMigrationOperationGenerator.fs b/src/EFCore.FSharp/Migrations/Design/FSharpMigrationOperationGenerator.fs index 0c04e8e..a02f1fe 100644 --- a/src/EFCore.FSharp/Migrations/Design/FSharpMigrationOperationGenerator.fs +++ b/src/EFCore.FSharp/Migrations/Design/FSharpMigrationOperationGenerator.fs @@ -48,24 +48,19 @@ type FSharpMigrationOperationGenerator (code : ICSharpHelper) = sb let annotations (annotations: Annotation seq) (sb:IndentedStringBuilder) = - annotations - |> Seq.iter(fun a -> - sb - |> appendEmptyLine - |> append (sprintf ".Annotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value)) - |> ignore - ) - sb + + let lines = + annotations + |> Seq.map (fun a -> sprintf ".Annotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value)) + + sb |> appendLines lines true let oldAnnotations (annotations: Annotation seq) (sb:IndentedStringBuilder) = - annotations - |> Seq.iter(fun a -> - sb - |> appendEmptyLine - |> append (sprintf ".OldAnnotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value)) - |> ignore - ) - sb + let lines = + annotations + |> Seq.map (fun a -> sprintf ".OldAnnotation(%s, %s)" (code.Literal a.Name) (code.UnknownLiteral a.Value)) + + sb |> appendLines lines true let generateMigrationOperation (op:MigrationOperation) (sb:IndentedStringBuilder) :IndentedStringBuilder = invalidOp ((op.GetType()) |> DesignStrings.UnknownOperation) @@ -375,7 +370,7 @@ type FSharpMigrationOperationGenerator (code : ICSharpHelper) = let writeConstraints sb = let hasConstraints = - notNull op.PrimaryKey || (op.UniqueConstraints |> Seq.isEmpty |> not) || (op.ForeignKeys |> Seq.isEmpty |> not) + notNull op.PrimaryKey && (op.UniqueConstraints |> Seq.isEmpty |> not) && (op.ForeignKeys |> Seq.isEmpty |> not) if hasConstraints then @@ -386,16 +381,14 @@ type FSharpMigrationOperationGenerator (code : ICSharpHelper) = |> ignore if notNull op.PrimaryKey then + + let pkName = op.PrimaryKey.Name |> code.Literal + let pkColumns = op.PrimaryKey.Columns |> Seq.map(fun c -> map.[c]) |> Seq.toList |> code.Lambda + sb - |> append "table.PrimaryKey(" - |> append (op.PrimaryKey.Name |> code.Literal) - |> append ", " - |> append (op.PrimaryKey.Columns |> Seq.map(fun c -> map.[c]) |> Seq.toList |> code.Lambda) - |> append ")" - |> indent + |> append (sprintf "table.PrimaryKey(%s, %s)" pkName pkColumns) |> annotations (op.PrimaryKey.GetAnnotations()) |> appendLine " |> ignore" - |> unindent |> ignore op.UniqueConstraints |> Seq.iter writeUniqueConstraint @@ -404,6 +397,7 @@ type FSharpMigrationOperationGenerator (code : ICSharpHelper) = sb |> unindent |> appendLine ") " + |> unindent else sb diff --git a/src/EFCore.FSharp/Scaffolding/FSharpDbContextGenerator.fs b/src/EFCore.FSharp/Scaffolding/FSharpDbContextGenerator.fs index f71c4bf..790aaaa 100644 --- a/src/EFCore.FSharp/Scaffolding/FSharpDbContextGenerator.fs +++ b/src/EFCore.FSharp/Scaffolding/FSharpDbContextGenerator.fs @@ -60,20 +60,12 @@ type FSharpDbContextGenerator let generateDbSet (sb:IndentedStringBuilder) (entityType : IEntityType) = let dbSetName = entityDbSetName entityType - let mutableName = "_" + dbSetName sb - |> appendLine "[]" - |> appendLine (sprintf "val mutable private %s : DbSet<%s>" mutableName entityType.Name) - |> appendLine (sprintf "member this.%s" dbSetName) - |> indent - |> appendLine (sprintf "with get() = this.%s" mutableName) - |> appendLine (sprintf "and set v = this.%s <- v" mutableName) - |> unindent + |> appendLine (sprintf "member val %s: DbSet<%s> = null with get, set" dbSetName entityType.Name) + |> appendEmptyLine |> ignore - sb.AppendLine() |> ignore - let generateDbSets (model:IModel) (sb:IndentedStringBuilder) = model.GetEntityTypes() diff --git a/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs b/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs index 799d85e..09bad21 100644 --- a/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs +++ b/tests/EFCore.FSharp.Tests/Migrations/Design/FSharpMigrationsGeneratorTest.fs @@ -639,8 +639,7 @@ type MyMigration() = inherit Migration() override this.Up(migrationBuilder:MigrationBuilder) = - migrationBuilder.Sql("-- TEST") - .Annotation("Some:EnumValue", RegexOptions.Multiline) |> ignore + migrationBuilder.Sql("-- TEST").Annotation("Some:EnumValue", RegexOptions.Multiline) |> ignore migrationBuilder.AlterColumn( name = "C1"