efcore / EFCore.FSharp

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

HasData seems to generate invalid code #80

Closed zelenij closed 3 years ago

zelenij commented 3 years ago

A simple type:


[<CLIMutable>]
type Country = 
    {
        [<Key>]
        [<MaxLength(3)>]
        IsoCode: string
        EnglishName: String
        NativeName: String
    }

Given a list of values, I add them in DbContext:


        override _.OnModelCreating(builder: ModelBuilder): unit = 
            // Add all countries to the DB
            for country in CountryUtils.getAllCountries do
                builder.Entity<Country>().HasData(country) |> ignore

            base.OnModelCreating(builder)

The generated migrations code looks invalid:


        modelBuilder.Entity("Omnicv.Common.Db.Country", (fun b ->

            b.Property<string>("IsoCode")
                .HasMaxLength(3)
                .HasColumnType("TEXT") |> ignore
            b.Property<string>("EnglishName")
                .HasColumnType("TEXT") |> ignore
            b.Property<string>("NativeName")
                .HasColumnType("TEXT") |> ignore

            b.HasKey("IsoCode") |> ignore

            b.HasIndex("IsoCode")
                .IsUnique() |> ignore

            b.ToTable("Countries") |> ignore

            b.HasData([| 
                ({
                    ,
                    IsoCode = "abw"EnglishName = "Aruba"NativeName = "Aruba"} :> obj)
                 |> ignore
                ({
                    ,
                    IsoCode = "afg"EnglishName = "Afghanistan"NativeName = "Afghanistan"} :> obj)
                 |> ignore
                ({
                    ,
                    IsoCode = "ago"EnglishName = "Angola"NativeName = "Angola"} :> obj)
                 |> ignore
                ({
                    ,
simon-reynolds commented 3 years ago

Hi @zelenij This should be resolved now by https://www.nuget.org/packages/EntityFrameworkCore.FSharp/5.0.3-alpha8

zelenij commented 3 years ago

Thanks, will give it a go a bit later! :)

flensrocker commented 3 years ago

Hi,
I'm using 5.0.3-alpha9. There seems to a little mistake at the end of each record.

Code generated:

values = array2D [ [ Guid("cd735892-e9a7-4b36-b910-03e97a95b421") :> obj; "user1" ] :> obj; [ Guid("ff7b5153-2218-45bc-9838-892b99f97b59") :> obj; "user2" ] ]

Should be:

values = array2D [ [ Guid("cd735892-e9a7-4b36-b910-03e97a95b421") :> obj; "user1" :> obj ]; [ Guid("ff7b5153-2218-45bc-9838-892b99f97b59") :> obj; "user2" :> obj ] ]

The :> obj cast is not inside the bracket. And it's missing at the last record.

Thank you!

flensrocker commented 3 years ago

And there is an error FS0039 in BuildTargetModel, in my case the Id property can't be resolved (I only have the german error message).
Maybe it should be an anonymous record at that place (that's what gets suggested as a fix)?

Sorry, I'm new to F#... :)

Generated:

b.HasData([| 
    { Id = Guid("cd735892-e9a7-4b36-b910-03e97a95b421"); Username = "user1"; }
    { Id = Guid("ff7b5153-2218-45bc-9838-892b99f97b59"); Username = "user2"; }
|]) |> ignore

Should be?

b.HasData([| 
    {| Id = Guid("cd735892-e9a7-4b36-b910-03e97a95b421"); Username = "user1"; |}
    {| Id = Guid("ff7b5153-2218-45bc-9838-892b99f97b59"); Username = "user2"; |}
|]) |> ignore
simon-reynolds commented 3 years ago

Hi @flensrocker You're absolutely right, that should be an anonymous record type in the HasData method

I've just released https://www.nuget.org/packages/EntityFrameworkCore.FSharp/5.0.3-alpha10 if you can take a look, should address both issues

flensrocker commented 3 years ago

I will take a look later this evening or tomorrow. Thank you!

flensrocker commented 3 years ago

Can't wait... :)

It works in my case!