gpt4 book ai didi

asp.net-mvc - EntityFramework 到 Json 的解决方法? (在序列化 ...DynamicProxies 类型的对象时检测到循环引用)

转载 作者:行者123 更新时间:2023-12-04 09:55:48 30 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Json and Circular Reference Exception

(9 个回答)


8年前关闭。




所以这是我的交易

型号

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)连接到以下存储库...

接口(interface)/存储库
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 方法,我想返回上下文。
这是方法

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”类型的对象时检测到循环引用。

我猜这与延迟加载的东西有关(我目前正在学习 C#)我发现这篇关于这个的文章......

http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/

但我没有让它工作......我能读到的关于代码的内容是他们正在尝试通过对象进行深度搜索......不仅仅是我无法弄清楚。

我的问题是如何传入lazyLoading 对象?进入 json/序列化器
或者它不存在,关于我如何进行的任何想法?

最佳答案

由于 Json 是一种基于树的序列化格式,它在 A->B->A 等引用方面存在问题。
我在某处读过你可以使用 ScriptIgnore View 模型中的属性以防止此错误。但是还没有测试过。

您可以将代码更改为以下(使用匿名类型)以成功检索项目:

 var p = newsRepository.AllIncluding(news => news.Category, news => news.Image)
.Select(n => new {id = n.Id, Body = n.Body});

在最后一个 Select 中包含您希望显示的任何其他属性方法。这也使您的 Json 结果更轻量级。

关于asp.net-mvc - EntityFramework 到 Json 的解决方法? (在序列化 ...DynamicProxies 类型的对象时检测到循环引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608372/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com