[英]Entity Framework return “$ref” items
I'm doing my homework - making an web api 2 project with entity framework. 我正在做作业-使用实体框架制作一个Web api 2项目。
Here is my table layout, with m-2-m relationship between Singer and Genre tables. 这是我的表布局,在Singer和Genre表之间具有m-2-m关系。
My Genre is a table which I Use to get my singers list by alphabet (so Genre table get data from (ID: 1, Name: "A") to (ID: 26, Name: "Z") 我的流派是用于按字母顺序获取歌手列表的表(因此,流派表可从(ID:1,名称:“ A”)到(ID:26,名称:“ Z”)获取数据
Now I want to get all singers start with "H" (id=8) 现在我要让所有歌手都以“ H”开头(id = 8)
public IQueryable<Singer> GetByGenre(int id)
{
return db.Singers.Where(s => s.Genres.Any(g => g.ID == id));
}
with this query: 与此查询:
http://localhost:6798/api/Singers/GetByGenre?id=8
But the result's strange - only the first result's show, while others return as "ref" ? 但是结果很奇怪-仅显示第一个结果,而其他返回“ ref”? I checked by create a "fake" list
我通过创建“假”列表进行检查
var list = db.Singers.Where(s => s.Genres.Any(g => g.ID == id)).ToList();
and put debug on it, and it return correct 3 Singers ? 并对其进行调试,它会返回正确的3位歌手?
-- -
And as you see, I have other table (Song, Playlist, Video) which have a FK point to ID of my Singer , so how can I only return the Singer table's columns only (ID, NAME, IMAGE) without all other relationship data ( Playlists, Songs, Videos ...) ? 如您所见,我还有另一个表(Song,Playlist,Video),它们的FK指向我的Singer的ID,所以我如何只返回Singer表的列(ID,NAME,IMAGE)而没有所有其他关系数据(播放列表,歌曲,视频...)?
Why does it include $ref in returned json result.
为什么在返回的json结果中包含$ ref。
It is Json serializer who is including $ref to resolve the circular dependancies. 是Json序列化器包含$ ref来解决循环依赖关系。
Look here for detail on this Stackoverflow answer -> Why is the Web Api returning empty json results with “$ref=”? 在此处查看有关Stackoverflow答案的详细信息-> 为什么Web Api会使用“ $ ref =”返回空的json结果?
how can I only return the Singer table's columns only (ID, NAME, IMAGE) without all other relationship data ( Playlists, Songs, Videos ...) ?
如何仅返回Singer表的列(ID,NAME,IMAGE)而没有其他所有关系数据(播放列表,歌曲,视频...)?
You need to disable your lazy loading in the entity framework dbcontext. 您需要在实体框架dbcontext中禁用延迟加载。
something like this way: 这样的事情:
db.Configuration.LazyLoadingEnabled = false;
For more information about lazy loading and serializer have a detailed look at this msdn article. 有关延迟加载和序列化程序的更多信息,请详细阅读此 msdn文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.