Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web API加载相关数据_C#_Entity Framework_Asp.net Core Webapi - Fatal编程技术网

C# Web API加载相关数据

C# Web API加载相关数据,c#,entity-framework,asp.net-core-webapi,C#,Entity Framework,Asp.net Core Webapi,我正在使用ASP CORE 2.1和EF CORE创建Web API,其中包含2个数据表和1个外键 型号: public partial class AuditInfo { public int Id { get; set; } public string Level { get; set; } public string Period { get; set; } public string Auditor { get; set; } public

我正在使用ASP CORE 2.1和EF CORE创建Web API,其中包含2个数据表和1个外键

型号:

 public partial class AuditInfo
{

    public int Id { get; set; }
    public string Level { get; set; }
    public string Period { get; set; }
    public string Auditor { get; set; }

    public virtual ICollection<Item> Items { get; set; }
}

  public partial class Item
{
    public int Id { get; set; }
    public string Ponumber { get; set; }
    public bool IsComplete { get; set; }

    public AuditInfo AuditInfo { get; set; }
}

public partial class VEDHOMEContext : DbContext
{
    public VEDHOMEContext()
    {

    }

    public VEDHOMEContext(DbContextOptions<VEDHOMEContext> options)
        : base(options)
    {

    }

    public virtual DbSet<AuditInfo> AuditInfo { get; set; }
    public virtual DbSet<Item> Item { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {




    }

     protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder
            .HasAnnotation("ProductVersion", "2.1.1-rtm-30846")
            .HasAnnotation("Relational:MaxIdentifierLength", 128)
            .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

        modelBuilder.Entity("auditAPI.Models.AuditInfo", b =>
        {
            b.Property<int>("Id")
                .ValueGeneratedOnAdd()
                .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

            b.Property<string>("Auditor");

            b.Property<string>("Level");

            b.Property<string>("Period");

            b.HasKey("Id");

            b.ToTable("AuditInfo");
        });

        modelBuilder.Entity("auditAPI.Models.Item", b =>
        {
            b.Property<int>("Id")
                .ValueGeneratedOnAdd()
                .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

            b.Property<int?>("AuditInfoId");

            b.Property<bool>("IsComplete");

            b.Property<string>("Ponumber");

            b.HasKey("Id");

            b.HasIndex("AuditInfoId");

            b.ToTable("Item");
        });

        modelBuilder.Entity("auditAPI.Models.Item", b =>
        {
            b.HasOne("auditAPI.Models.AuditInfo", "AuditInfo")
                .WithMany("Items")
                .HasForeignKey("AuditInfoId");
        });


    }



}
预期产出:

[{"id":1,"level":"level1","period":"jan","auditor":"A","items":{"Id":1,"Ponumber":"0001","IsComplete":"True","AuditInfoId":1},{"id":2,"Ponumber":"0002","IsComplete":"True","AuditInfoId":1}}]
对于有类似问题的人,我通过添加

services.AddMvc()
.AddJsonOptions(options => {
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
编辑控制器

public async Task<List<AuditInfo>> GetAuditInfo()
    {
        //return _context.AuditInfo;
        var infoes = await _context.AuditInfo.Include(a => a.Items).ToListAsync();
        return infoes;

    }
public异步任务GetAuditInfo()
{
//返回_context.AuditInfo;
var infoes=await_context.AuditInfo.Include(a=>a.Items.toListSync();
返回信息;
}

我不确定您是否看到了公认的答案,但问题在于JSON序列化程序如何处理循环引用。在上面的链接中可以找到更多引用的完整详细信息和链接,我建议深入研究这些内容,但简而言之,在
startup.cs
中添加以下内容将配置序列化程序以忽略循环引用:

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

这正是正确的答案,难以置信的容易解决@侯赛因你让我开心!
public async Task<List<AuditInfo>> GetAuditInfo()
    {
        //return _context.AuditInfo;
        var infoes = await _context.AuditInfo.Include(a => a.Items).ToListAsync();
        return infoes;

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