2013-12-11 22 views
0

首先我想告訴你我的架構。 我對故事的表故事類Asp.Net Mvc Web Api Json格式化程序問題

public class Tale 
{ 
    [ScaffoldColumnAttribute(false)] 
    public int TaleId { get; set; } 
    public string TaleName { get; set; } 
    public string Content { get; set; } 
    public string VoicePath { get; set; } 
    public virtual ICollection<Category> Category { get; set; } 
} 

我對分類表

public class Category 
{ 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
    public virtual ICollection<Tale> Tales { get; set; } 
} 

喜歡,我類別類創建我TalesCategory表

modelBuilder.Entity<Tale>() 
      .HasMany<Category>(u => u.Category) 
      .WithMany(r => r.Tales) 
      .Map(c => c.ToTable("TalesCategory") 
         .MapLeftKey("TaleKey") 
         .MapRightKey("CategoryKey")); 

及其我TaleController:ApiController功能

public IEnumerable<Tale> GetAllTales() 
    { 
     return TaleService.FindAllTale(); 
    } 

我WebApiConfig

config.Formatters.Clear(); 
     config.Formatters.Add(new JsonMediaTypeFormatter()); 

如果我寫「/ API /故事」我想我的故事的名單,但我把這個錯誤。我想我必須寫JsonMediaFormatter,我嘗試了一些代碼,但是沒有成功。我能做什麼?我使用.Net Framework 4.5。

{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Tale_B48C4EAA8B3983ECA938C57C1764611B3C06FAC3348891DAC636EBEBF05EA8E2'. Path '[0].Category[0].Tales'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n konum: 
+1

可能重複http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop -detected-type) – CodeCaster

回答

0

由於EF類中的導航屬性,您需要打破正在拾取的循環引用。

嘗試

public IEnumerable<Tale> GetAllTales() 
{ 
    return TaleService.FindAllTale().Select(x=> new Tale 
        { 
         TaleId = x.TaleId, 
         TaleName = x.TaleName, 
         ... 
        }); 
} 
[類型檢測錯誤JSON.NET自參考環(的
+0

我明白了,但是我想在這個結果中看到類別id。我編輯我的代碼與你的例子{TaleId = x.TaleId,... Category = x.Category}和ı帶這個錯誤「在System.Data.Entity.dll中發生類型'System.Data.EntityCommandExecutionException'的異常但沒有在用戶代碼中處理「。我在ICollection 上遇到了一些問題。我的大腦有點困惑,因爲我第一次使用Code First ... – BerdaN

+0

也使用「Select」作爲分類。像這樣:'Category = x.Category.Select(y => new Category {CategoryId = y.CategoryId,...}) –

+0

感謝您的回答,但現在我需要轉換類型錯誤。正如我所說的Category是ICollection <>「不能隱式轉換類型'System.Collections.Generic.IEnumerable '爲'System.Collections.Generic.ICollection '。顯式轉換「) – BerdaN