簡體   English   中英

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

[英]Json response does not contain all the navigation properties EntityFramework Core and ASP .NETCore Web API

我已經從Entity Framework 6遷移到EF Core,也從Web Api .net框架遷移到了.net core。

我建立了如下的多對多關系

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”時,在發送響應之前放置斷點時,我得到了預期的結果,如下所示

在此處輸入圖片說明

如您所見,Navigation屬性已加載(當我展開集合時,我也可以看到所有屬性都已加載)。 但是我收到的json響應如下

在此處輸入圖片說明

有什么建議還是我在這里錯過了什么?

謝謝大家

謝謝@H。 Herzl給我一個提示。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM