così heres l'affare hoSoluzione da EntityFramework a JSON? (Un riferimento circolare rilevato durante la serializzazione di un oggetto di tipo ... DynamicProxies)
Modelli
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; }
}
.... seguenti modelli (questi modelli sono collegati al EfDbContext) collegato al seguente repository ...
Interfaccia/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();
}
Nel mio HomeController() ho ottenuto un metodo JsonResult che voglio restituire il contesto. Ecco il metodo
JSON Richiesta
[HttpGet]
public JsonResult GetNews()
{
var p = newsRepository.AllIncluding(news => news.Category, news => news.Image);
return Json(p, JsonRequestBehavior.AllowGet);
}
ottengo il seguente errore:
stato rilevato un riferimento circolare mentre la serializzazione di un oggetto di tipo System.Data.Entity.DynamicProxies' .News_96C0B16EC4AC46070505EEC7537EF3C68EE6CE5FC3C7D8EBB793B2CF9BD391B3' .
ho intuito che questo ha qualcosa a che fare con la roba lazyloading (Iam attualmente apprendimento su C#) ho trovato questo articolo su questo ...
http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/
ma mi hanno dato per funzionare. .. quello che ho potuto leggere sul codice era che stavano cercando di approfondire la ricerca attraverso l'oggetto ... più di quello che non riuscivo a capire.
la mia domanda è come posso passare in oggetti lazyLoading? in json/serializer o non esiste, qualche pensiero su come posso procedere?
grazie per la risposta !, sì ho risolto come hai fatto tu, ma volevo usare la dinamica (lazyloading), per ottenere il risultato JSON dal suo lavoro non prezioso per ricreare il modello nel controller, questo è quello che penso ... beh proverò se ScriptIgnore funziona .. grazie! – Martea
@Martea: Nessun problema. Se pensi che questa sia la risposta alla tua domanda, contrassegnala come risposta. – Kamyar