Brian
Brian

Reputation: 151

Many to Many Relationship query EF Core

I am using an ASP.NET Core 2.1 API application and EF Core 2.1

I have a many to many relationship between Movies and Actors with the generated column MovieActors that has a movie Id and an actor Id.

I need to return a List with movies that doesn't return all attributes, but only posterUrl and release. It also has to return a list with actor id's corresponding with each movie.

I have something like this

        var movies = _context.Movies  
            // Actors
            .Include(ma => ma.Actors)
            .ThenInclude(a => a.ActorId)    
            .AsNoTracking()
            .AsQueryable();

This doesn't work because it says that ActorId is no navigation property.

How do I include only the actor Id's (so not the whole actor) and select only the posterUrl and release from the movies.

Upvotes: 0

Views: 453

Answers (1)

Cetin Basoz
Cetin Basoz

Reputation: 23797

var movies = _context.Movies
            .Select(m => new {m.MovieName, 
                 m.MovieYear, 
                 ..., 
                 Actors = m.MovieActors.Select(ma => ma.ActorId)})   
            .AsNoTracking()
            .AsQueryable();

Upvotes: 1

Related Questions