[英]Entity Framework: Exclude a navigation property
I'm using Entity Framework 6 and I'd like to retrieve an entity which has several navigation properties. 我正在使用Entity Framework 6,并且想检索一个具有多个导航属性的实体。 I do not want to retrieve one of them and I didn't find a way to achieve this. 我不想检索其中的一个,也没有找到实现此目的的方法。
Here's my model: 这是我的模型:
Full size image: http://i.imgur.com/KFdsTVU.jpg?1 原图: http : //i.imgur.com/KFdsTVU.jpg?1
I want to retrieve all navigation properties of a Session entity except Discussions. 我想检索“讨论”以外的Session实体的所有导航属性。 So I did: 所以我做了:
using (var context = new ModelContainer())
{
context.Database.Log = msg => Trace.WriteLine(msg);
var session = await
context.SessionSet.FirstOrDefaultAsync(a => a.Identifier == sessionIdentifier);
var result = await Json(session).ExecuteAsync(new CancellationToken());
return ResponseMessage(result);
}
But I would expect from Entity Framework to have a method which excludes fields of an entity. 但是我希望从Entity Framework提供一种排除实体字段的方法。
I ended up doing this: 我最终这样做:
var session = await
context.SessionSet.Select(a => new
{
Host = a.Host,
Identifier = a.Identifier,
Destination = a.Destination,
Positions = a.Positions,
Sentinelles = a.Sentinelles,
Id = a.Id,
Code = a.Code,
Notifications = a.Notifications,
Status = a.Status,
Transportation = a.Transportation
}).FirstOrDefaultAsync(a => a.Identifier == sessionIdentifier);
var result = await Json(session).ExecuteAsync(new CancellationToken());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.