RESTful API Entity Framework 上下文重复子集合数据 C#
RESTful API Entity Framework context repeating child collections data C#
我正在使用投影(显式加载)来调用和过滤从子 table 返回的集合数据,结果通过 RESTful Api 服务公开。 json 很好,但它重复了子 table 集合对象。使用 "AsNoTracking()" 方法会使 json 输出中的嵌套子集合无效,这是不正确的。
下面是我的代码和 json 输出。我需要帮助来阻止子对象重复
public IQueryable<ApiViewModel> getAllActive()
{
//Explicit loading (projection)
var result = db.Markets
.Where(p => p.IsActive == true)
.Select(p => new ApiViewModel()
{
Market = p,
TravelCentres = p.TravelCentres.Where(x => x.IsActive == true)
});
return result;
}
JSON 输出:
[
{
"Market": {
"MarketId": "AE",
"Name": "Arabian Emirates",
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
},
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
}
]
尝试 Distinct() 方法。
另见上文 link
在MSDN
我能够通过使用预加载和第 3 方 IncludeFilter() 扩展方法来解决问题。
https://github.com/zzzprojects/EntityFramework-Plus/issues
我正在使用投影(显式加载)来调用和过滤从子 table 返回的集合数据,结果通过 RESTful Api 服务公开。 json 很好,但它重复了子 table 集合对象。使用 "AsNoTracking()" 方法会使 json 输出中的嵌套子集合无效,这是不正确的。
下面是我的代码和 json 输出。我需要帮助来阻止子对象重复
public IQueryable<ApiViewModel> getAllActive()
{
//Explicit loading (projection)
var result = db.Markets
.Where(p => p.IsActive == true)
.Select(p => new ApiViewModel()
{
Market = p,
TravelCentres = p.TravelCentres.Where(x => x.IsActive == true)
});
return result;
}
JSON 输出:
[
{
"Market": {
"MarketId": "AE",
"Name": "Arabian Emirates",
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
},
"TravelCentres": [
{
"City": "New Lynn, New Zealand",
"Address": "now",
"Telephone": "09169647771",
"Email": "test@wak.com"
},
{
"City": "Uyo, Nigeria",
"Address": "Ewet housing",
"Telephone": null,
"Email": null
},
{
"City": "Lagos, Nigeria",
"Address": "no",
"Telephone": "09993",
"Email": "patricko@wak.com"
}
]
}
]
尝试 Distinct() 方法。
另见上文 link
在MSDN
我能够通过使用预加载和第 3 方 IncludeFilter() 扩展方法来解决问题。 https://github.com/zzzprojects/EntityFramework-Plus/issues