Json響應不包含EntityFramework Core和ASP .NETCore Web API的所有導航屬性

asp.net-core-webapi entity-framework-core

我已經從Entity Framework 6遷移到EF Core以及Web Api .net框架到.net核心。

我有很多很多關係,我建立如下

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var instrumentsToPlaces = modelBuilder.Entity<InstrumentPlace>();
        instrumentsToPlaces.ToTable("InstrumentsToPlaces");
        instrumentsToPlaces.HasKey(x => new { x.PlaceId, x.InstrumentId });

        instrumentsToPlaces.HasOne(i => i.Instrument)
            .WithMany(p => p.InstrumentsPlaces)
            .HasForeignKey(ip => ip.InstrumentId);

        instrumentsToPlaces.HasOne(p => p.Place)
            .WithMany(i => i.InstrumentsPlaces)
            .HasForeignKey(ip => ip.PlaceId);


        var instrumentsToStyle = modelBuilder.Entity<InstrumentStyle>();
        instrumentsToStyle.ToTable("InstrumentsToStyles");
        instrumentsToStyle.HasKey(x => new { x.StyleId, x.InstrumentId });

        instrumentsToStyle.HasOne(i => i.Instrument)
            .WithMany(s => s.InstrumentStyles)
            .HasForeignKey(si => si.InstrumentId);

        instrumentsToStyle.HasOne(s => s.Style)
            .WithMany(i => i.InstrumentStyles)
            .HasForeignKey(si => si.StyleId);

    }

我已在存儲庫方法中包含導航屬性,如下所示

        public Instrument GetInstrumentByName(string name)
    {
        using (var starsAndCatzDbContext = new StarsAndCatzDbContext())
        {
            var instrument = _starsAndCatzDbContext.Instruments
            .Include(a=>a.InstrumentsPlaces)
            .ThenInclude(a=>a.Place)
            .Include(a=>a.InstrumentStyles)
            .ThenInclude(a=>a.Style)
           .FirstOrDefault(i => i.Name == name);


            return instrument;

        }


    }

這是課程

public class Instrument {
    public int Id { get; set; }
    public string Name { get; set; }

    public  virtual ICollection<InstrumentPlace> InstrumentsPlaces { get; set; }
    public virtual ICollection<InstrumentStyle> InstrumentStyles { get; set; }
}

 public class InstrumentPlace
{
    public int InstrumentId { get; set; }
    public Instrument Instrument { get; set; }
    public int PlaceId { get; set; }
    public Place Place { get; set; }
}

     public class InstrumentStyle
{
    public int InstrumentId { get; set; }
    public Instrument Instrument { get; set; }
    public int StyleId { get; set; }
    public Style Style { get; set; }
}

    public class Style {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<InstrumentStyle> InstrumentStyles { get; set; } 
}

        public class Place {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Division { get; set; }
    public int Tier { get; set; }
    public string State { get; set; }
    public string Postcode { get; set; }
    public float? Latitude { get; set; }
    public float? Longitude { get; set; }
    public virtual ICollection<InstrumentPlace> InstrumentsPlaces { get; set; }
}

要調用的WebAPI方法是

        [HttpGet("GetInstrumentByName/{suburb}/{instrument}"), Produces("application/json")]
    public Instrument GetInstrumentByName(string suburb, string instrument)
    {
        try
        {
            var result = _instrumentRepository.GetInstrumentByName(instrument);
            return result;
        }
        catch (Exception ex)
        {

            _logger.LogError(ex.Message);
            return new Instrument();
        }

    }

當我將請求發送到“/ api / instruments / west-end / guitar”時,我在發送響應之前放置斷點時獲得預期結果,如下所示

在此處輸入圖像描述

如您所知,導航屬性已加載(當我展開集合時,我可以看到所有正在加載的屬性)。但是我收到的json響應如下

在此處輸入圖像描述

有什麼建議或我在這裡遺漏了什麼?

先謝謝大家

熱門答案

謝謝@H。赫茲爾給了我一個提示。

在另一個問題中找到了解決方案

services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

https://stackoverflow.com/a/40501464/1513346



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow