简体   繁体   English

EF6 Linq Query。仅包含返回子表的第一个条目

[英]EF6 Linq Query .Include only returning first entry of child table

I would like to retrieve a list of Encounters for a Given Pokemon, as a Pokemon can be encountered in many places, so I've been trying many variants of 我想检索一个给定的口袋妖怪的邂逅列表,因为口袋妖怪可以在很多地方遇到,所以我一直在尝试很多变种

var currentPokemon = _context.Pokemon
                                     .Where(mon => mon.Id == id)
                                     .Include(mon => mon.Encounters)
                                     .FirstOrDefault();

With the result being a Pokemon object with all of the relevant data, but only the FIRST encounter being retrieved and put into a collection resulting in this: 结果是一个包含所有相关数据的Pokemon对象,但只有FIRST遭遇被检索并放入一个集合中,结果如下:

在此输入图像描述

Looking at the database, there are about 20 encounters for caterpie, and I'd like access to all of them, but only ever just get the one. 看看这个数据库,大约有20次遇到caterpie,我想要访问所有这些,但只是得到了它。

What the Pokemon class looks like (irrelevant fields omitted): Pokemon类的外观(省略了相关字段):

[Table("pokemon")]
public partial class Pokemon {
    public Pokemon()
    {
        Encounters = new HashSet<Encounters>();
    }
    [Column("id")]
    public long Id { get; set; }
    [Required]
    [Column("identifier", TypeName = "VARCHAR(79)")]
    public string Identifier { get; set; }
    .
    .
    .

    [InverseProperty("Pokemon")]
    public virtual ICollection<Encounters> Encounters { get; set; }
 }

What Encounters looks like: 邂逅的样子:

public partial class Encounters {
    .
    .
    .
    [ForeignKey("PokemonId")]
    [InverseProperty("Encounters")]
    public virtual Pokemon Pokemon { get; set; }
}

Db data : Db数据:

在此输入图像描述

What am I misunderstanding here? 我在这里误解了什么?

I think its because you are calling .FirstOrDefault() , which is only getting the first item. 我认为这是因为你正在调用.FirstOrDefault() ,它只获取第一个项目。 Can you omit that and add .ToList() ? 你可以省略并添加.ToList()吗? Also, var currentPokemon doesn't seem like a good variable name. 另外, var currentPokemon似乎不是一个好的变量名。 You said you want a list of Encounters, right? 你说你想要一份遭遇者名单,对吧? How about var pokemonEncounters ? var pokemonEncounters怎么var pokemonEncounters

I think that the problem is the way that you set the relationship using ForeignKey and InverseProperty. 我认为问题在于您使用ForeignKey和InverseProperty设置关系的方式。

Try to rewrite to something like: 尝试重写为:

[Table("Pokemon")]
public class Pokemon
{
    public int Id { get; set; }
    public string Name { get; set; }

    [InverseProperty("Pokemon")]
    public ICollection<Encounter> Encounters { get; set; }

}

[Table("Enconters")]
public class Encounter
{
    public int Id { get; set; }

    public int PokemonId { get; set; }

    [ForeignKey("PokemonId")]
    public Pokemon Pokemon { get; set; }

}

My apologies, I've been looking to the wrong places for my answer. 我道歉,我一直在寻找错误的地方寻找答案。 After stepping through this in a debugger, I can see that my Pokemon query does indeed return the desired result: A pokemon with many encounters attached. 在调试器中单步执行此操作后,我可以看到我的Pokemon查询确实返回了所需的结果:附加了许多遭遇的口袋妖怪。 My problem seems to be elsewhere (specifically: the JSONified object I'm seeing come through to my web front-end truncates the encounters array to contain only the first result). 我的问题似乎在其他地方(特别是:我看到的JSONified对象通过我的Web前端截断遇到的数组只包含第一个结果)。

I'll post a new question with the correct problem and link to it from here when it's figured out. 我将发布一个带有正确问题的新问题,并在计算出来时从这里链接到它。

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

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