Ygor Alberto
Ygor Alberto

Reputation: 61

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 Farms. 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.

Upvotes: 0

Views: 211

Answers (1)

Ygor Alberto
Ygor Alberto

Reputation: 61

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]
                }

Upvotes: 1

Related Questions