so heres the deal i have
Models
public class News
{
public News()
{
this.Created = DateTime.Now;
}
public int Id { get; set; }
public string Title { get; set; }
public string Preamble { get; set; }
public string Body { get; set; }
public DateTime Created { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public int CategoryId { get; set; }
public int ImageId { get; set; }
public virtual Image Image { get; set; }
public virtual Category Category { get; set; }
}
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public Byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
....following models(these models are connected to the EfDbContext) connected to following repository...
Interface/Repository
public class NewsRepository : INewsRepository
{
EfDbContext context = new EfDbContext();
public IQueryable<News> All
{
get { return context.News; }
}
public IQueryable<News> AllIncluding(params Expression<Func<News, object>>[] includeProperties)
{
IQueryable<News> query = context.News;
foreach (var includeProperty in includeProperties) {
query = query.Include(includeProperty);
}
return query;
}
public News Find(int id)
{
return context.News.Find(id);
}
public void InsertOrUpdate(News news)
{
if (news.Id == default(int)) {
// New entity
context.News.Add(news);
} else {
// Existing entity
context.Entry(news).State = EntityState.Modified;
}
}
public void Delete(int id)
{
var news = context.News.Find(id);
context.News.Remove(news);
}
public void Save()
{
context.SaveChanges();
}
}
public interface INewsRepository
{
IQueryable<News> All { get; }
IQueryable<News> AllIncluding(params Expression<Func<News, object>>[] includeProperties);
News Find(int id);
void InsertOrUpdate(News news);
void Delete(int id);
void Save();
}
In my HomeController() i got a a JsonResult metod that i want to return the context. Here is the Method
Json Request
[HttpGet]
public JsonResult GetNews()
{
var p = newsRepository.AllIncluding(news => news.Category, news => news.Image);
return Json(p, JsonRequestBehavior.AllowGet);
}
I get the following error:
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.News_96C0B16EC4AC46070505EEC7537EF3C68EE6CE5FC3C7D8EBB793B2CF9BD391B3'.
I guessed that this has something to do with the lazyloading stuff(Iam currently learning about C#) i found this article about this...
http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/
but i didnt get it to work... what i could read about the code was that they were tryin to depth search trough the object... more than that i couldn't figure out.
my question is how to i can pass in lazyLoading objects? into json/serializer or does it not exist, any thoughts of how i can proceed?