简体   繁体   English

基于“一对多”的反向关系映射使用实体框架和 GraphQl

[英]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.我正在开发我的 API 的新版本,现在使用 GraphQL 来获得一些优势。

I have the entity Partner which has a List property of Farm s.我有实体Partner ,它具有FarmList属性。 On that point is ok, when I call Partners on my API I can get their Farms .在这一点上是可以的,当我在我的 API 上调用Partners ,我可以获得他们的Farms

But when I call Farms in my API, I want it to return the Partner related to it, and there is my issue.但是当我在 API 中调用Farms时,我希望它返回与之相关的Partner ,这就是我的问题。

I tried using InverseProperty annotation on Partner beyond ForeignKey on Farm , but I get the following error message for every attibute of Farm :我尝试在Farm上的ForeignKey之外的Partner上使用InverseProperty注释,但是对于Farm每个属性,我都收到以下错误消息:

Expected to find property [attribute] on Int32 but it does not exist.期望在 Int32 上找到属性[attribute]但它不存在。

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 .这是一个遗留代码,因为它不遵循代码约定,我也不能更改Farm Partner属性。

Found the solution while posting the doubt (like Rubber Duck Debugging).在发布疑问时找到了解决方案(如 Rubber Duck Debugging)。

On Schema, the attributes names must be the same of the class (seems obvious, I know, but take care).在 Schema 上,属性名称必须与类相同(我知道这似乎很明显,但请注意)。 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]
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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