无法将 poco 对象从 EF 4 转换为 JSON

发布于 2024-09-15 12:32:20 字数 530 浏览 3 评论 0原文

我正在使用实体框架开发 mvc 2.0 应用程序。在实体框架中,我将存储库模式与 poco 对象一起使用。首先解决这个问题,当我将实体对象转换为 json 时,出现循环引用错误。

经过一番搜索,我发现生成了代理来支持延迟加载。如果两个类(A 和 B)之间存在导航属性,则会导致循环引用错误。很好理解。所以我尝试解决这个问题。

我禁用了代理和延迟加载。如果我只想加载 A 类,则此方法有效。现在不再是代理,而是空值,因此可以解析它们。

但现在我想加载一个类,例如 Orders,我想查看哪个客户下了订单:

假设我有一个 Customer 类,它有一个到 Order 的导航属性(1 到更多),而 Order 有一个到 Customer 的反向导航属性。当我关闭代理时,我会收到一个包含所有订单的漂亮 json,但不会包含客户。当我打开代理时,出现循环错误。

但我怎样才能向购买订单的客户取回订单呢?是否可以创建一个 linq 来检索订单并加载客户(我有一个用于客户和订单的存储库)?或者有没有办法去掉代理对象?

我希望我的帖子足够清楚,有人可以帮助我。

Im working on a mvc 2.0 application using the entity framework. With the entityframework I use the repository pattern with poco objects. To start off with the issue, when I convert an entity object to json I get a circular reference error.

After some search I discovered that there are proxy's generated to support lazy loading. If there are navigation properties between two classes (A and B), this results in a circurar reference error. Quite understandable. So I try to work around it.

I disabled the proxies and the lazy loading. This works if I only want to load Class A. Instead of the proxy's there are now null values, so they can be parsed.

But now I want to load a class, for instance Orders and I want to see what customer placed the order:

Suppose I have class Customer that has a navigation property to Order (1 to more) and Order has a reversed navigation property to Customer. When I turn the proxys off, I get a nice json back with all the orders, but not with the Customers. When I turn the proxies on, I get a circular error.

But how could I get back the orders, with the customer that bought them. Is it possible to create a linq that retreives the orders and load the customers (I have a repository for both customers and orders)? Or is there a way to strip off the proxy-objects?

I hope my post is clear enoug and someone can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-09-22 12:32:20

问题:
正确的。所以你有关系 A -> B,其中 B 是多方。
在 EF 模型中,A 获取导航属性 B,B 获取导航属性 A。
循环引用...太棒了...

解决方案:
在您的模型中,右键单击 B 的导航属性 A 并选择属性。
getter 和 setter 最初都应该是公开的。将 getter 设置为 Private。

现在这样的事情应该可以工作了。

var results = from a in ctx.A.Include("B")
select a;

var list = results.ToList(); //This is important otherwise youll get a error that the context has been disposed on the next line.
return Json(list, JsonRequestBehavior.AllowGet);

希望这有帮助。

附:
发布后阅读我的答案后,我不再确定我是否真正回答了您的问题,抱歉。尽管如此,我还是把它保留下来。

Problem:
Right. So you have the relationship A -> B with B being the many side.
in the EF model A gets a navigation property B and B gets a navigation property A.
Circular reference... great...

Solution:
In your model, rightclick on the B's navigation property A and choose properties.
Getter and setter there should both be public initially. Set the getter to Private.

Now something like this should work.

var results = from a in ctx.A.Include("B")
select a;

var list = results.ToList(); //This is important otherwise youll get a error that the context has been disposed on the next line.
return Json(list, JsonRequestBehavior.AllowGet);

Hope this helps.

PS:
After reading my answer after posting it, I'm not so sure anymore that I'm really answering your question, sorry. Ill leave it up nonetheless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文