1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sujon Chondro Shil
  • 105
  • 1
  • 2
  • 10
  • You don't - EF already does that for you - automagically, in the background – marc_s Jan 21 '18 at 21:09
  • how will EF know which genres are for the movie? – Sujon Chondro Shil Jan 21 '18 at 21:19
  • I have already created list of genres and stored them in the database table.When admin register new movie he need to select the genres of the movie. so from view section i get the information of the movie and the array of genres. how can i store them ? – Sujon Chondro Shil Jan 21 '18 at 21:25

1 Answers1

0

The code should look more like this:

var movie = db.Movies.Add(data);

var genre = db.Genres.Add(data);

movie.Genres.Add(movie);

db.SaveChanges();

You'll see that the third line answers your remaining question: how will EF know which genres are for the movie?

But, since you used a Code-First approach then you will also need:

  1. ICollections on both models specifying the many-to-many relationship (you already have this).
  2. To configure the many-to-many relationship within the OnModelCreating of your DbContext using the Fluent API. You can read more about this here.

For those using a Database-First approach (using Entity Framework Power Tools):

Your edmx (Entity Data Model XML) file will automagically create the relationship for you (no manual additions to the DbContext required) with a caveat:

Entity framework supports many-to-many relationships only when the joining table does NOT include any columns other than PKs of both tables. http://www.entityframeworktutorial.net/entity-relationships.aspx

You'll know that your relationship is setup correctly when EFPT has put the correct ICollections on your models, and your edmx file doesn't show your pivot table at all -- instead, your edmx file shows a direct many-to-many relationship ( denoted by a * on both ends ) between your two tables.

Design.Garden
  • 3,607
  • 25
  • 21