I have two models, Movie
and Genre
.
Movie class:
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string File { get; set; }
[Required]
public DateTime Release { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
Genre class:
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string Name { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
And this is the CineDBContext
context class
public virtual DbSet<Movie> Movies { get; set; }
public virtual DbSet<Genre> Genres { get; set; }
I have used a code-first approach to generate the database and tables. As there has many to many relationships three tables are generated in the database Movies
, Genres
and GenreMovies
.
Inside controller class:
private CineDBContext db = new CineDBContext();
db.Movies.Add(data); //Fills the Movies table
db.SaveChanges();
db.Genres.Add(data); //Fills the Genres table
db.SaveChanges();
But how can I fill the GenreMovies
pivot table?