2016-12-29 4 views
0

エンティティフレームワーク6からEFコアに移行し、Web Api .netフレームワークを.netコアに移行しました。私はここでJsonレスポンスにすべてのナビゲーションプロパティが含まれていないEntityFrameworkコアおよびASP .NETCore Web 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; 

     } 


    } 

を次のように私は、リポジトリ方式のナビゲーションプロパティが含まれている

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 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メソッドは、

です。私は「/ API /楽器/ウエストエンド/ギター」にリクエストを送信すると、私は

enter image description here

ように、次のように応答を送信する前にブレークポイントを置いたときに、私は期待どおりの結果を得る

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

    } 

ナビゲーションプロパティがロードされます(コレクションを展開すると、ロードされているすべてのプロパティも表示されます)。 私が受け取るJSONレスポンスは、任意の提案、次の

enter image description here

であるか、私はここで何かが足りないのですか?しかし、

ありがとうございました。

+0

で発見されたあなたは、インストゥルメント・クラス定義をしてください投稿することができますか? –

+0

'public class Instrument { \t \t public int Id {get;セット; } \t \t public string Name {get;セット; } \t \t公開リスト InstrumentsPlaces {get;セット; } 公開リスト InstrumentStyles {get;セット; } } ' 私は前に仮想ICollectionとしてコレクションを持っていましたが、それが問題だが何も起こらなかったかどうかを確認するために変更しました – carlosJ

+1

私は同様の機能を持っていて、私はパブリックバーチャルコレクションを使用していますOrderDetails {get;セット; }私はナビゲーションプロパティを持つエンティティを取得します。私はシリアライザの設定を確認する必要があると思います。 –

答えて

1

ありがとうございます。私にヒントを与えてくれたヘルツル。

ソリューションは、この他の質問

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

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

関連する問題