2016-12-29 71 views
0

我已經從實體框架6遷移到EF Core以及Web Api .net框架到.net核心。Json響應不包含所有導航屬性EntityFramework核心和ASP .NETCore Web API

我有多對多的關係,我已設置如下

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 /儀表/西區/吉他:」我得到預期的結果,當我發送響應之前,放置一個斷點如下

enter image description here

由於你注意到,導航屬性被加載(當我展開集合時,我可以看到所有正在加載的屬性)。 但是我收到的JSON響應爲以下

enter image description here

任何建議還是我失去了一些東西?

謝謝大家先進

+0

您可以發佈儀表類的定義嗎? –

+0

'public class Instrument { \t \t public int Id {get;組; } \t \t public string Name {get;組; } \t \t public List InstrumentsPlaces {get;組; } public List InstrumentStyles {get;組; }} 」 我有收藏像以前虛擬的ICollection ..但改變它,看看如果是這樣的問題,但什麼都沒有發生 – carlosJ

+1

我有一個類似的功能,我使用公共虛擬集合訂單明細{獲得;組; }我得到的導航屬性的實體,我認爲你需要檢查你的序列化設置,你檢查? –

回答

1

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

該解決方案在此的其他問題被發現

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

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