Сначала я хочу рассказать о своей архитектуре. Мой класс сказка для Tale столаAsp.Net Mvc Web Api Json Formatter issue
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());
, если я пишу «/ апи/сказания» Я хочу, чтобы список моей истории, но я беру эту ошибку. Я думаю, что мне нужно написать 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:
возможно дубликат [Json.NET самообслуживания Ошибка ссылающейся петли для обнаруженного типа] (http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop -detected-for-type) – CodeCaster