Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:31:52+00:00 2026-05-26T09:31:52+00:00

I’m using fluent syntax to configure a M:M relationship between simple Movie and Genre

  • 0

I’m using fluent syntax to configure a M:M relationship between simple Movie and Genre entities. That’s working OK.

My problem is that I also wish to expose the link/join table MovieGenre as an entity as well. I realize it’s not necessary since it only contains keys, but I wish to have it because it will make some operations easier.

I get this error when I try to configure the link table:

The EntitySet ‘MovieGenre1’ with schema ‘Media’ and table ‘MovieGenre’
was already defined.

I know it has to do w/ me already setting up the M:M relationship but haven’t figured out how to make it all work together.

Below is my code:

public class Genre
{
   public int GenreID { get; set;}
   public string Name { get; set; }

   public ICollection<Movie> Movies { get; set; }
}

public class Movie
{
   public int MovieID { get; set; }
   public string Title { get; set; }

   public ICollection<Genre> Genres { get; set; }
}

public class MovieGenre
{
   public int MovieID { get; set; }
   public int GenreID { get; set; }

   public Movie Movie { get; set; }
   public Genre Genre { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   //Genre
   modelBuilder.Entity<Genre>()
               .ToTable("Genre", "Media");

   //Movie
   var movieCfg = modelBuilder.Entity<Movie>();
   movieCfg.ToTable("Movie", "Media");
   movieCfg.HasMany(m => m.Genres)
           .WithMany(g => g.Movies)
           .Map(m =>
           {
              m.MapLeftKey("MovieID");
              m.MapRightKey("GenreID");
              m.ToTable("MovieGenre", "Media");
           });

   //MovieGenres
   var movieGenresCfg = modelBuilder.Entity<MovieGenre>();
   movieGenresCfg.ToTable("MovieGenre", "Media");
   movieGenresCfg.HasKey(m => new
   {
      m.MovieID, m.GenreID
   });

   base.OnModelCreating(modelBuilder);
}

Any help would be appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:31:53+00:00Added an answer on May 26, 2026 at 9:31 am

    What you are trying is not possible. You need to remove your MovieGenre from the mapping.

    Do not worry about fetching the entity from the database just to add it to the list of Genres. If you load an entity by id multiple times there is only one call to the DB anyway. As EF gets better and a second level cache is added it will just work and perform well. If you are too imperative there is no magic left to the framework.

    And if you think about the domain and the users of your model, most of it is reads anyway so not many updates. On top of it adding a movie to a genres or the other way around could mean many things in which case you should not expose the collections directly but add methods to to implement the behaviours.

    One example of behaviour would be “if you add a movie to more than 10 genres then the movie should change the say its category to X” and all sorts of other things. Stop thinking about data only. Domain Driven Design is a good book to read on the subject.

    In your case here you can consider the Movie entity as a root aggregate because a Movie is what your users are after and a Movie is what you are going to edit as an administrator. A movie can exist without a Genre, maybe, so genre is just a tag on the movies, a value object, and not very important. Your mappings would become:

        public class Genre
        {
           public int GenreID { get; set;}
           public string Name { get; set; }
        }
    
        public class Movie
        {
           public int MovieID { get; set; }
           public string Title { get; set; }
    
           public ICollection<Genre> Genres { get; set; }
        }
    ....
    
        //Movie
       var movieCfg = modelBuilder.Entity<Movie>();
       movieCfg.ToTable("Movie", "Media");
       movieCfg.HasMany(m => m.Genres)
               .WithMany()
               .Map(m =>
               {
                  m.MapLeftKey("MovieID");
                  m.MapRightKey("GenreID");
                  m.ToTable("MovieGenre", "Media");
               });
    

    The queries would look like:

    // get all movies for a particular genre
    var movies = movieContext.Movies.Where(m => m.Genres.Any(g => g.GenreID = id))
                                    .OrderBy(m => m.Name)
                                    .ToList();
    
    // get all movies 
    var movies = movieContext.Movies.OrderBy(m => m.Name).Take(10).ToList();
    

    This is pretty much all you are going to do with your domain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.