简体   繁体   中英

Inverse relationship mapping based on "one to many" using Entity Framework and GraphQl

I'm working on a new version of my API, now using GraphQL for some advantages.

I have the entity Partner which has a List property of Farm s. On that point is ok, when I call Partners on my API I can get their Farms .

But when I call Farms in my API, I want it to return the Partner related to it, and there is my issue.

I tried using InverseProperty annotation on Partner beyond ForeignKey on Farm , but I get the following error message for every attibute of Farm :

Expected to find property [attribute] on Int32 but it does not exist.

Here is part of my code:

public class Partner 
{
    ...
    [InverseProperty("PartnerOwner")]
    public List<Farm> Farms{ get; set; }
}

public class Farm 
{
    ...
    [Key]
    public int Codigo { get; set; }

    [ForeignKey("PartnerOwner")]
    public int Partner { get; set; }

    public Partner PartnerOwner { get; set; }
}

Schema:

type Propriedade 
{
    Codigo: ID,
    ...
    Parceiro: Parceiro
}

type Partner 
{
    Codigo: ID,
    ...
    CountryFarms: [Farm]
}

Is there some detail I'm missing on that development?

It's a legacy code, because of that it doesn't follow code conventions neither I can change Partner property of Farm .

Found the solution while posting the doubt (like Rubber Duck Debugging).

On Schema, the attributes names must be the same of the class (seems obvious, I know, but take care). On this case specifically the Schema seems like this to work fine:

type Propriedade {
                    Codigo: ID,
                    ...
                    Parceiro: Parceiro
                }

type Partner {
                    Codigo: ID,
                    ...
                    // Here is the trick
                    Farms: [Farm]
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM