包含关系(PK,FK)的EF实体不能被Jsoned? [英] EF entities that contain relations (PKs,FKs) cannot be Json-ed?

查看:105
本文介绍了包含关系(PK,FK)的EF实体不能被Jsoned?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  **门票**:
TicketId(PK )
AreaId(FK)
SeverityId(FK)
AssigneeId(FK)

**区域**:
AreaId(PK)
AreaName

**严重性**:
SeverityId(PK)
SeverityName

**受让人**:
AssigneeId(PK )
AssigneeName

我创建了实体模型,在TicketController中,我试图检索和Json在门票表中的数据。

  [HttpPost] 
public ActionResult索引([DataSourceRequest] DataSourceRequest结果)
{
var tickets = db.Tickets.Include(t => t.Areas).Include(t => t.Assignees).Include(t => t.Severities)

DataSourceResult result = tickets.ToDataSourceResult(request);

返回Json(结果);
}

出于某种原因我无法理解我Json的结果为了通过它在KendoUiGrid的视图中,我得到循环引用错误。 我在表/实体关系中看不到循环引用!



在Kendo文档中,我阅读了这个


如果您决定直接使用Kendo UI JavaScript库,
返回JSON很简单。您只需要定义一个控制器
方法,并在返回结果上调用this.Json。提供的.NET可以
序列化您的响应,这是所有的。


以上所有都意味着现实我不能从具有主键/外键的理智数据库模式中得到Json实体?



提前Thanx

解决方案

创建一个平面的Model类,并将模型传递给Json和Grid。您将不得不手动填充模型,而不是您在这里尝试做的更多,但还有其他好处可以让您稍后发现一个模型。循环引用是因为您的EF类的导航属性。在MVC中,该模型并不意味着数据模型类,而是您创建的新类。做好正确的新模式应该做大部分的工作,验证,翻译等。控制器只是传递的东西,视图只是渲染。希望这有帮助!


I have build a very very simple database schema as

**Tickets**:
TicketId (PK)
AreaId (FK)
SeverityId (FK)
AssigneeId (FK)

**Areas**:
AreaId(PK)
AreaName

**Severities**:
SeverityId(PK)
SeverityName

**Assignees**:
AssigneeId(PK)
AssigneeName

I create the Entity Model and in the TicketController I am trying to retrieve and Json the data inside the Tickets table.

[HttpPost]
public ActionResult Index([DataSourceRequest]DataSourceRequest result)
{
var tickets = db.Tickets.Include(t=> t.Areas).Include(t=> t.Assignees).Include(t=> t.Severities)

DataSourceResult result = tickets.ToDataSourceResult(request);

Return Json(result);
}

For a reason that I cannot understand when I Json the result in order to pass it to a KendoUiGrid in the view, I get the "circular reference error". I see no circular reference in my tables/entities relations!!

In the Kendo documentation I read this

If you decide to use the Kendo UI JavaScript libraries directly, returning JSON is simple. You just need to defined a controller method and call this.Json on your return result. Provided .NET can serialize your response, this is all there is to it.

Does all the above mean that in reality I cannot Json entities that come from sane database schemas with primary/foreign keys?

Thanx in advance

解决方案

Create a flat Model class and pass the model to Json and Grid. You will have to populate the model a little more manually than what you are trying to do here, but there are other benefits to having a model that you will discover later. The circular reference is because of the navigational properties of your EF classes. In MVC the model does not mean the data model classes, but new ones you create. Done right the new model should do most of the work, validation, translation, etc. The controllers just passes things around, the view just renders. Hope this helps!

这篇关于包含关系(PK,FK)的EF实体不能被Jsoned?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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