2016-12-29 2 views
0

Entity Framework 6에서 EF Core로, Web Api .net 프레임 워크에서 .net 코어로 마이그레이션했습니다. I 여기Json 응답에 모든 탐색 속성이 포함되어 있지 않습니다. EntityFramework 코어 및 ASP .NETCore 웹 API

 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; 

     } 


    } 

다음과 같이 I 저장소 방법 탐색 특성을 포함했다

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); 

    } 

을 다음 I가 설정 한 많은 관계 많은가

는 클래스이다

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(); 
     } 

    } 

내가 요청을 보낼 때에 내가 응답을 보내기 전에 중단 점을 배치 할 때

enter image description here

으로 다음과 같이 "/ API/악기/서쪽 끝/기타"나는 예상되는 결과를 얻을 수 네비게이션 속성이로드됩니다 (컬렉션을 확장하면로드되는 모든 속성을 볼 수 있음). 내가받은 JSON 응답은 다음과

enter image description here

어떤 제안이나 내가 여기서 뭔가를 놓치고있다 그러나?

모두 고맙습니다.

+0

당신이 악기 클래스 정의를하시기 바랍니다 게시 할 수이 다른 질문에서 발견되었다? –

+0

'공용 클래스 악기 { \t \t public int Id {get; 세트; } \t \t public string Name {get; 세트; } \t \t 공개 목록 InstrumentsPlaces {get; 세트; } 공개 목록 InstrumentStyles {get; 세트; } } ' 이전에 가상 ICollection 컬렉션이 있었지만 문제가 발생했는지 알 수 없도록 변경했습니다. 아무 일도 없었습니다. – carlosJ

+1

비슷한 기능이 있으며 공용 가상 컬렉션을 사용합니다. OrderDetails {get; 세트; } 탐색 속성이있는 엔티티를 얻었 으면 serializer 설정을 확인해야한다고 생각합니까? –

답변

1

고맙습니다. @H. 헤즐은 나에게 힌트를 주었다.

솔루션

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

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