efcore / EFCore.FSharp

Adds F# design-time support to EF Core
MIT License
228 stars 26 forks source link

Migration not generated due to optional single case DU #142

Open brud opened 1 year ago

brud commented 1 year ago

Describe the bug Migration not generated due to single DU (PageId)

To Reproduce

module Database

open System.ComponentModel.DataAnnotations
open System.ComponentModel.DataAnnotations.Schema
open Microsoft.EntityFrameworkCore
open EntityFrameworkCore.FSharp.Extensions
open Microsoft.EntityFrameworkCore.Design

type ContentId = ContentId of int
type PageId = PageId of int

// some other entities

[<CLIMutable>]
type Content = {
    [<Key>] Id: ContentId
    PageId: PageId option
    ParentId: int option
    [<NotMapped>]
    Parent: Content optional
}

type ConstructorContext(options : DbContextOptions<ConstructorContext>) =
    inherit DbContext(options)

    [<DefaultValue>] val mutable contents : DbSet<Content>
    member this.Contents with get() = this.contents and set v = this.contents <- v

    override _.OnModelCreating builder =
        builder.RegisterOptionTypes() // enables option values for all entities
        builder.RegisterSingleUnionCases() // enables single case unions for all entities

    override _.OnConfiguring(options: DbContextOptionsBuilder) : unit =
        options
            .UseNpgsql(connectionString)
            .UseFSharpTypes() // enable queries for F# types
        |> ignore

type ApplicationDbContextFactory() =
    interface IDesignTimeDbContextFactory<ConstructorContext> with
        member _.CreateDbContext (_: string[]) =
            let optionsBuilder = DbContextOptionsBuilder<ConstructorContext>()
            optionsBuilder.UseNpgsql(connectionString)
            |> ignore
            new ConstructorContext(optionsBuilder.Options)

I have an Exception after dotnet ef migrations add Init

The property 'Content.PageId' is of type 'PageId' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder. Ignore' in 'OnModelCreating'.

UPD.: as I can see, this error appears only if field is single DU AND option

UPD. [2]: when I try to create Parent optional field (with NotMapped attribute) i get an error: The property 'Content.Parent' is of type 'Content' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder .Ignore' in 'OnModelCreating'. But if I remove option - the errror dissapears.

brud commented 1 year ago

Any updates?