Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to implement many to many relation with include in generic repository by params in EF Core.
I have these models:
C#
public class Movie:IEntityBase
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
 
    public MovieCategory MovieCategory { get; set; }
    public string? ImageURL { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public decimal Price { get; set; }

    public ICollection<MovieActor> MovieActors { get; set; }
    public ICollection<MovieCinema> MovieCinemas { get; set; }

    public int MovieDetailId { get; set; }
    [ForeignKey("MovieDetailId")]
    public MovieDetail MovieDetail { get; set; }
}

public class Cinema:IEntityBase
{
    [Key]
    public int Id { get; set; }
    [DisplayName("لوگو")]
    public string? Logo { get; set; }
    [NotMapped]
    [Required(ErrorMessage = ErrorMsg.RequiredMsg)]
    public IFormFile? PicUpload { get; set; }
    [DisplayName("نام سینما")]
    [Required(ErrorMessage = ErrorMsg.RequiredMsg)]

    public string Name { get; set; }

    [Required(ErrorMessage = ErrorMsg.RequiredMsg)]
    [DisplayName("توضیحات")]
    public string Description { get; set; }
    public ICollection<MovieCinema>? MovieCinemas { get; set; }
}

public class MovieCinema
{
    public int MovieId { get; set; }
    public int CinemaId { get; set; }
    [ForeignKey("MovieId")]
    public Movie Movie { get; set; }
    [ForeignKey("CinemaId")]
    public Cinema Cinema { get; set; }
}

IEntityBaseRepository
C#
Task<IEnumerable<T>> GetAll(params string[] includeProperties);

EntityBaseRepository
C#
public async Task<IEnumerable<T>> GetAll(params string[] includeProperties)
{
    IQueryable<T> query = _context.Set<T>();

    query = includeProperties.Aggregate(query, 
    (current, includeProperty) => current.Include(includeProperty));

    return await query.ToListAsync();
}

wrote this code in the MoviesController:
C#
var allMovies = await _service.GetAll(includeProperties: "MovieCinema,Cinema");

but I get this error:
InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError': Unable to find navigation 'MovieCinema' specified in string based include path 'MovieCinema'. This exception can be suppressed or logged by passing event ID 'CoreEventId.InvalidIncludePathError' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.


What I have tried:

How to write code in the MoviesController index action to solve this problem and access to Name of Cinema at Movie index.
Posted
Updated 19-Oct-23 5:03am
v2

1 solution

The error you are encountering is likely because the MovieCinema property is a collection in the Movie class, not a direct navigation property. You should include the MovieCinemas.Cinema property instead. Additionally, ensure that your context is properly set up and configured. Here's how you can modify your code:
In your MoviesController index action, modify your code to include the MovieCinemas.Cinema property:
C#
public async Task<IActionResult> Index()
{
    var allMovies = await _service.GetAll(includeProperties: "MovieCinemas.Cinema");

    // Use the allMovies data as needed in your view or further processing

    return View(allMovies);
}

In your EntityBaseRepository, modify the GetAll method to include the Include method properly:
C#
public async Task<IEnumerable<T>> GetAll(params string[] includeProperties)
{
    IQueryable<T> query = _context.Set<T>();

    foreach (var includeProperty in includeProperties)
    {
        query = query.Include(includeProperty);
    }

    return await query.ToListAsync();
}

With these modifications, the Include method should properly handle the navigation property, and you should be able to access the Name of the Cinema in the Movie index. Make sure your database context and configurations are set up correctly to avoid any additional issues.
 
Share this answer
 
Comments
Member 16118565 21-Oct-23 15:39pm    
Thanks a lot . Problem solved.The answer is :
var allMovies = await _service.GetAll(includeProperties: "MovieCinemas.Cinema")
Member 16118565 21-Oct-23 15:40pm    
Thanks a lot . Problem solved .The answer is :
var allMovies = await _service.GetAll(includeProperties: "MovieCinemas.Cinema")
M Imran Ansari 21-Oct-23 16:47pm    
Wonderful, glad to hear that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900