E.F Core 在包含另一个表时不返回所有值

huangapple go评论70阅读模式
英文:

E.F Core does not return all values When Include another table

问题

以下是您要翻译的代码部分:

public IEnumerable<Parties> GetAll()
{
    return database.Parties;
}

这段代码很有效,输出如下:

[![enter image description here][1]][1]

但是,当我通过外键包含另一张表时,像这样:

public IEnumerable<Parties> GetAll()
{
    return database.Parties.Include(i=>i.User);
}

它不起作用,它只返回表的第一个值,输出如下:

[![enter image description here][2]][2]

Users.cs:

public partial class Users
{
    public Users()
    {
        Parties = new HashSet<Parties>();
        PartyParticipants = new HashSet<PartyParticipants>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string Avatar { get; set; }
    public string Biography { get; set; }
    public string Password { get; set; }

    public virtual ICollection<Parties> Parties { get; set; }
    public virtual ICollection<PartyParticipants> PartyParticipants { get; set; }
}

Parties.cs:

public partial class Parties
{
    public Parties()
    {
        Image = new HashSet<Image>();
        PartyParticipants = new HashSet<PartyParticipants>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime PartyDate { get; set; }
    public DateTime CreatedDate { get; set; }
    public int ParticipantCount { get; set; }
    public int MaxParticipant { get; set; }
    public string PartySplash { get; set; }
    public string ShortDescription { get; set; }
    public string Description { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public bool EntranceFree { get; set; }
    public int? FreeParticipant { get; set; }
    public int? FreeParticipantMax { get; set; }
    public int UserId { get; set; }

    public virtual Users User { get; set; }
    public virtual ICollection<Image> Image { get; set; }
    public virtual ICollection<PartyParticipants> PartyParticipants { get; set; }
}

正如您在第二张图片中所看到的,它在表的第一行中中断。

E.F Core 在包含另一个表时不返回所有值

请注意,这是您提供的代码的翻译,不包括问题的回答。

英文:
   public IEnumerable&lt;Parties&gt; GetAll()
    {
        return database.Parties;
    }

Works very well and the output is:
E.F Core 在包含另一个表时不返回所有值

But when I Include another table by foreignkey like this:

  public IEnumerable&lt;Parties&gt; GetAll()
    {
        return database.Parties.Include(i=&gt;i.User);
    }

It does not work, it returns first value of the table and nothing else,the output is : E.F Core 在包含另一个表时不返回所有值

Users.cs :

  public partial class Users
{
    public Users()
    {
        Parties = new HashSet&lt;Parties&gt;();
        PartyParticipants = new HashSet&lt;PartyParticipants&gt;();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public string Avatar { get; set; }
    public string Biography { get; set; }
    public string Password { get; set; }

    public virtual ICollection&lt;Parties&gt; Parties { get; set; }
    public virtual ICollection&lt;PartyParticipants&gt; PartyParticipants { get; set; }
}

Parties.cs :

  public partial class Parties
{
    public Parties()
    {
        Image = new HashSet&lt;Image&gt;();
        PartyParticipants = new HashSet&lt;PartyParticipants&gt;();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime PartyDate { get; set; }
    public DateTime CreatedDate { get; set; }
    public int ParticipantCount { get; set; }
    public int MaxParticipant { get; set; }
    public string PartySplash { get; set; }
    public string ShortDescription { get; set; }
    public string Description { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public bool EntranceFree { get; set; }
    public int? FreeParticipant { get; set; }
    public int? FreeParticipantMax { get; set; }
    public int UserId { get; set; }

    public virtual Users User { get; set; }
    public virtual ICollection&lt;Image&gt; Image { get; set; }
    public virtual ICollection&lt;PartyParticipants&gt; PartyParticipants { get; set; }
}

As you can see on the 2nd picture it interrupts at first row of the table.
E.F Core 在包含另一个表时不返回所有值

答案1

得分: 0

我已根据Vidmantas的评论添加了这个答案。在startup.cs中应该这样忽略ReferenceLoopHandling

services.AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
英文:

I have added this answer based on Vidmantas's comment. ReferenceLoopHandling should be ignored like this in startup.cs:

services.AddMvc()
    .AddJsonOptions(options =&gt;
    {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });

huangapple
  • 本文由 发表于 2020年1月4日 00:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定