简体   繁体   中英

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. 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

I want to retrieve all navigation properties of a Session entity except Discussions. 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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM