的EntityFramework到JSON解决方法吗? (而序列化类型的对象时检测到循环引用... DynamicProxies) [英] EntityFramework to Json workaround? ( A circular reference was detected while serializing an object of type...DynamicProxies)

查看:128
本文介绍了的EntityFramework到JSON解决方法吗? (而序列化类型的对象时检测到循环引用... DynamicProxies)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,继承人的处理我有

so heres the deal i have

模式

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)...

....following models(these models are connected to the EfDbContext) connected to following 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();
}

在我的HomeController()我得到了一个JsonResult梅托德,我要返回的上下文。
这里是法

In my HomeController() i got a a JsonResult metod that i want to return the context. Here is the Method

JSON请求

    [HttpGet]
    public JsonResult GetNews()
    {
        var p = newsRepository.AllIncluding(news => news.Category, news => news.Image);
        return Json(p, JsonRequestBehavior.AllowGet);
    }

我收到以下错误:

I get the following error:

在序列化类型的对象时检测到循环引用'System.Data.Entity.DynamicProxies.News_96C0B16EC4AC46070505EEC7537EF3C68EE6CE5FC3C7D8EBB793B2CF9BD391B3'.

我猜这事做的惰性加载的东西(IAM目前学习C#)我发现这篇文章这个...

I guessed that this has something to do with the lazyloading stuff(Iam currently learning about C#) i found this article about this...

<一个href=\"http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/\">http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/

但我没有得到它的工作...我可以阅读有关code的,他们试着深度搜索槽的对象......不止这些我无法弄清楚。

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.

我的问题是如何,我可以在惰性加载的对象传递?成JSON /串行
或者它不存在,怎么我可以进行什么想法?

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?

推荐答案

由于JSON是基于树的序列化格式,它具有像A-> B-> A引用的问题。结果
我读过的地方,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute.aspx\">ScriptIgnore在你的ViewModels归因于prevent此错误。但没有测试过。

Since Json is a tree-based serialization format, it has problems with references like A->B->A.
I've read somewhere that you can use ScriptIgnore attribute in your viewmodels to prevent this error. But have not tested it.

您可以更改code以下(使用匿名类型)成功地检索项目:

You can change your code to the following (use anonymous types) to retrieve the items successfully:

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

包含要在最后一个选择方法来显示所有的其他财产。这使得您的JSON结果更为轻巧了。

Include any other property you wish to display in the last Select method. This makes your Json results more lightweight too.

这篇关于的EntityFramework到JSON解决方法吗? (而序列化类型的对象时检测到循环引用... DynamicProxies)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆