Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve discovery for F# explicit fields #12923

Open
Tracked by #22952
ctaggart opened this issue Aug 7, 2018 · 2 comments
Open
Tracked by #22952

Improve discovery for F# explicit fields #12923

ctaggart opened this issue Aug 7, 2018 · 2 comments
Labels
area-F# area-model-building area-proxies customer-reported help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-enhancement
Milestone

Comments

@ctaggart
Copy link

ctaggart commented Aug 7, 2018

I'm trying to use EF Core 2.1 from F# with lazy loading proxies, but keep running into:

No field was found backing property 'hypervisor' of entity type 'Alert'. Lazy-loaded navigation properties must have backing fields. Either name the backing field so that it is picked up by convention or configure the backing field to use.

When we follow the recommended workaround to add abstract fields, they end up with an appended @ symbol.

In this case, I have:

    [<ForeignKey("HypervisorID")>]
    default val hypervisor : Hypervisor = null with get, set
    abstract member hypervisor : Hypervisor with get, set

If view it in JustDecompile to C# or similar app, the field is:

internal Hypervisor hypervisor@;

        [ForeignKey("HypervisorID")]
        public override Hypervisor hypervisor
        {
            get
            {
                return this.hypervisor@;
            }
            set
            {
                this.hypervisor@ = value;
            }
        }

This is the behavior of F# explicit fields.

All the fields I have are this way. Is there a way to map F# explicit fields so that EF 2.1 lazy loaded proxies work? This doesn't work, but this was the direction I was going:

    override this.OnModelCreating builder =
        base.OnModelCreating builder
        for entity in builder.Model.GetEntityTypes() do
            for property in entity.GetProperties() do
                sprintf "%s@" property.FieldInfo.Name |> property.SetField
@ctaggart
Copy link
Author

ctaggart commented Aug 7, 2018

This appears to be working for me:

    // F# explicit fields append an `@` symbol to the backing fields
    // https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/members/explicit-fields-the-val-keyword
    override this.OnModelCreating builder =
        base.OnModelCreating builder
        for entity in builder.Model.GetEntityTypes() do
            for foreignKey in entity.GetForeignKeys() do
                foreignKey.DependentToPrincipal.Name |> sprintf "%s@" |> foreignKey.DependentToPrincipal.SetField
            for property in entity.GetProperties() do
                property.Name |> sprintf "%s@" |> property.SetField
            for navigation in entity.GetNavigations() do
                navigation.Name |> sprintf "%s@" |> navigation.SetField

@ctaggart ctaggart closed this as completed Aug 7, 2018
@ajcvickers ajcvickers reopened this Aug 8, 2018
@ajcvickers
Copy link
Member

Re-opening to consider adding this pattern to the field-matching convention.

@ajcvickers ajcvickers added this to the 2.2.0 milestone Aug 8, 2018
@ajcvickers ajcvickers self-assigned this Aug 8, 2018
@ajcvickers ajcvickers modified the milestones: 2.2.0, Backlog Aug 8, 2018
@ajcvickers ajcvickers added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Sep 2, 2019
@AndriySvyryd AndriySvyryd changed the title EF 2.1 lazy loaded proxies for F# explicit fields Improve discovery for F# explicit fields Oct 20, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-F# area-model-building area-proxies customer-reported help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants