First ı want to tell u my architecture. My tale class for Tale table
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; }
}
My category class for Category Table
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Tale> Tales { get; set; }
}
Like that ı create my TalesCategory table
modelBuilder.Entity<Tale>()
.HasMany<Category>(u => u.Category)
.WithMany(r => r.Tales)
.Map(c => c.ToTable("TalesCategory")
.MapLeftKey("TaleKey")
.MapRightKey("CategoryKey"));
And Its My TaleController:ApiController function
public IEnumerable<Tale> GetAllTales()
{
return TaleService.FindAllTale();
}
My WebApiConfig
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
if I write "/api/tales" ı want to list of my tale but ı take this error. I think I have to write JsonMediaFormatter and I try some code but ı didnt success. What can I do? I use .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:
You need to break the circular reference that is being picked up because of the navigational property in your EF class.
Try