实体框架4.1,MVC3 JsonResult和循环引用 [英] Entity Framework 4.1, MVC3 JsonResult and Circular References

查看:194
本文介绍了实体框架4.1,MVC3 JsonResult和循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习实体框架code首先开发ASP.NET MVC3。

让我们说我有一个拍卖和投标一个简单的数据模型,我想查询所有的拍卖和出价。

我已经关闭LazyLoadingEnabled和ProxyCreationEnabled。

下面是code我有:

 公共类MiCoreDb2Context:的DbContext
{
    公共MiCoreDb2Context()
        :基地()
    {
        this.Configuration.LazyLoadingEnabled = FALSE;
        this.Configuration.ProxyCreationEnabled = FALSE;
    }    公共DbSet<拍卖与GT;拍卖{搞定;组; }
    公共DbSet<出价>投标{搞定;组; }
}公共类拍卖
{
    公众诠释AuctionId {搞定;组; }
    公共虚拟的ICollection<出价>投标{搞定;组; }
}公共类投标
{
    众长BidId {搞定;组; }
    公众诠释AuctionId {搞定;组; }    [ForeignKeyAttribute(AuctionId)]
    公共虚拟拍卖拍卖{搞定;组; }
}
公共JsonResult事()
{
    清单<拍卖与GT;拍卖;    使用(VAR DB =新MiCoreDb2Context())
    {
        VAR拍卖=(来自于db.Auctions.Include(招标)选择).ToList();
    }    返回JSON(拍卖,JsonRequestBehavior.AllowGet);
}

当我加载页面,出现循环引用。我将如何解决此得到什么?


解决方案

  

当我加载页面,出现循环引用。我将如何解决此得到什么?


通过使用视图模型(以及这就是回答任何问题的方式,您可能已经涉及ASP.NET MVC :-))。 Ayende Rahien有<一个href=\"http://ayende.com/Blog/archive/2011/04/10/refactoring-toward-frictionless-amp-odorless-$c$c-the-case-for-the.aspx\">excellent系列博客文章的关于这一主题。

结论:绝对总是传递/从视图采取视图模型/。绝对不会通过/采取模型(EF,域...)/从一个视图。一旦这个基本规则被尊重,你会发现一切正常。

I'm trying to learn Entity Framework Code First development with ASP.NET MVC3.

Let's say I have a simple data Model for an Auction and Bids and I'd like to query all the Auctions and their Bids.

I have turned off LazyLoadingEnabled and ProxyCreationEnabled.

Here is the code I have:

public class MiCoreDb2Context : DbContext
{
    public MiCoreDb2Context()
        : base()
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    public DbSet<Auction> Auctions { get; set; }
    public DbSet<Bid> Bids { get; set; }
}

public class Auction
{
    public int AuctionId { get; set; }
    public virtual ICollection<Bid> Bids { get; set; }
}

public class Bid
{
    public long BidId { get; set; }
    public int AuctionId { get; set; }

    [ForeignKeyAttribute("AuctionId")]
    public virtual Auction Auction { get; set; }
}


public JsonResult Thing()
{
    List<Auction> auctions;

    using (var db = new MiCoreDb2Context())
    {
        var auctions = (from a in db.Auctions.Include("Bids") select a).ToList();
    }

    return Json(auctions, JsonRequestBehavior.AllowGet);
}

When I load the page, a circular reference occurs. How will I get around this?

解决方案

When I load the page, a circular reference occurs. How will I get around this?

By using view models (and by the way that's the answer to any question you might have concerning ASP.NET MVC :-)). Ayende Rahien has an excellent series of blog posts on this topic.

Conclusion: absolutely always pass/take view models to/from a view. Absolutely never pass/take models (EF, domain, ...) to/from a view. Once this fundamental rule is being respected you will find out that everything works.

这篇关于实体框架4.1,MVC3 JsonResult和循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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