C# 当包含另一个表时,E.F Core不会返回所有值 public IEnumerable GetAll() { 返回数据库。缔约方; }
工作正常,输出为: 但当我包含foreignkey的另一个表时,如下所示:C# 当包含另一个表时,E.F Core不会返回所有值 public IEnumerable GetAll() { 返回数据库。缔约方; },c#,asp.net,frameworks,entity,C#,Asp.net,Frameworks,Entity,工作正常,输出为: 但当我包含foreignkey的另一个表时,如下所示: public IEnumerable<Parties> GetAll() { return database.Parties; } public IEnumerable GetAll() { 返回database.Parties.Include(i=>i.User); } 它不工作,它返回表的第一个值,而不返回其他值,输出为: Users.cs: public
public IEnumerable<Parties> GetAll()
{
return database.Parties;
}
public IEnumerable GetAll()
{
返回database.Parties.Include(i=>i.User);
}
它不工作,它返回表的第一个值,而不返回其他值,输出为:
Users.cs:
public IEnumerable<Parties> GetAll()
{
return database.Parties.Include(i=>i.User);
}
公共部分类用户
{
公共用户()
{
Parties=新的HashSet();
PartyParticipants=新哈希集();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串Avatar{get;set;}
公共字符串{get;set;}
公共字符串密码{get;set;}
公共虚拟ICollection参与方{get;set;}
公共虚拟ICollection PartyParticipants{get;set;}
}
缔约方:
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; }
}
公共部分类参与方
{
公众团体()
{
Image=newhashset();
PartyParticipants=新哈希集();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间部分日期{get;set;}
公共日期时间CreatedDate{get;set;}
公共int ParticipantCount{get;set;}
public int MaxParticipant{get;set;}
公共字符串PartySplash{get;set;}
公共字符串ShortDescription{get;set;}
公共字符串说明{get;set;}
公共双纬度{get;set;}
公共双经度{get;set;}
公共布尔自由{get;set;}
公共int?自由参与者{get;set;}
公共int?FreeParticipantMax{get;set;}
public int UserId{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟ICollection映像{get;set;}
公共虚拟ICollection PartyParticipants{get;set;}
}
正如您在第二张图片上看到的,它在表的第一行中断。
我根据维德曼塔斯的评论添加了这个答案
ReferenceLoopHandling
应该像在startup.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; }
}
我根据维德曼塔斯的评论补充了这个答案
ReferenceLoopHandling
应该像在startup.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; }
}
它返回了用户,您希望返回什么?您是否希望使用用户所在的所有参与方填充参与方?如果是这样的话,你必须把它也包括在内。另外,在发布您的类声明时,我们不知道属性和类型。它不会返回所有内容,因为在第二张图片中,它在聚会上中断了。我们无法从图片中分辨任何内容,因为您在聚会上剪切了它。下一行应该是用户所在的parties对象,因为您的json编写器有问题,因为它不完整,因为并非所有的大括号和方括号都是闭合的。因此,问题可能不在EF中,而是在显示结果的方式中。这可能是因为
ReferenceLoopHandling
。。。确保已将其配置为忽略它返回给用户,您希望返回什么?您是否希望使用用户所在的所有参与方填充参与方?如果是这样的话,你必须把它也包括在内。另外,在发布您的类声明时,我们不知道属性和类型。它不会返回所有内容,因为在第二张图片中,它在聚会上中断了。我们无法从图片中分辨任何内容,因为您在聚会上剪切了它。下一行应该是用户所在的parties对象,因为您的json编写器有问题,因为它不完整,因为并非所有的大括号和方括号都是闭合的。因此,问题可能不在EF中,而是在显示结果的方式中。这可能是因为ReferenceLoopHandling
。。。确保将其配置为忽略