asp.net-mvc – 对Json解决方法的EntityFramework? (序列化类型对象时检测到循环引用… D
|
参见英文答案 >
Json and Circular Reference Exception9个
楷模 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; }
}
….以下模型(这些模型连接到EfDbContext)连接到以下存储库… 接口/库 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();
}
在我的HomeController()中,我得到了一个JsonResult metod,我想返回上下文. Json请求 [HttpGet]
public JsonResult GetNews()
{
var p = newsRepository.AllIncluding(news => news.Category,news => news.Image);
return Json(p,JsonRequestBehavior.AllowGet);
}
我收到以下错误: 序列化“System.Data.Entity.DynamicProxies.News_96C0B16EC4AC46070505EEC7537EF3C68EE6CE5FC3C7D8EBB793B2CF9BD391B3”类型的对象时检测到循环引用. 我猜这与懒加载的东西有关(Iam目前正在学习C#)我发现这篇文章关于这个…… http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/ 但我没有得到它的工作…我能读到的关于代码的是他们试图深入搜索通过对象…更多我无法弄清楚. 我的问题是如何传递lazyLoading对象?进入json / serializer 解决方法由于Json是基于树的序列化格式,因此它具有诸如A-> B-> A的引用的问题.我在某处读过你可以在viewmodels中使用 ScriptIgnore属性来防止这个错误.但还没有测试过. 您可以将代码更改为以下(使用匿名类型)以成功检索项目: var p = newsRepository.AllIncluding(news => news.Category,news => news.Image)
.Select(n => new {id = n.Id,Body = n.Body});
包括您希望在最后一个Select方法中显示的任何其他属性.这使得你的Json结果也更加轻量级. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- iis – 如何调试w3wp.exe随机崩溃的原因?
- asp.net-mvc – ASP.NET MVC ActionResult背后的推理是一个
- asp.net-web-api – 无法从’Microsoft.IdentityModel.Toke
- asp.net-mvc – MVC数据类型ErrorMessage
- asp.net – 来自web.config的Asp .Net自定义成员资格参数
- asp.net – WCF安全 – 我不明白的列表
- asp.net-mvc – HTTP错误500.19 – 内部服务器错误 – syst
- asp.net-web-api – 如何在HttpReponseMessage上设置响应co
- 从Asp.Net Webforms显示引导模式
- asp.net下创建、查询、修改带名称空间的 XML 文件的例子
- asp.net-mvc – 在ASP.NET MVC ViewModel类中获取
- asp.net – “此操作需要IIS集成管道模式
- asp.net-mvc – ASP.NET MVC:下载excel文件
- asp.net – 使用UpdatePanel的CollectionPager问
- asp.net-mvc – MVC 5:Asp.net身份:如何建模Us
- entity-framework – 在Junction表上使用EF Core
- asp.net-mvc – ASP.NET MVC 4覆盖发出的html名称
- asp.net-mvc – ASP.NET MVC3 – 3层设计 – 事务
- asp.net-web-api – 如何从Web API响应中删除标头
- asp.net – 存储Web应用程序项目组合参考的位置?
