EF Core include many to many relation in the generic repository by params

55 views Asked by At

How implement many to many relation with include in generic repository by params in EF Core.

Link below is a type of my question but not my question .

ef-core-include-for-many-to-many-in-the-generic-repository

I have theses model classes:

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 :

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

EntityBaseRepository :

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

I wrote this code in the MoviesController:

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

How to write code in the MoviesController index action to solve this problem?

0

There are 0 answers