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 8963423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:21:06+00:00 2026-06-15T16:21:06+00:00

This is all refactorable, so if there’s a better way to approach the situation

  • 0

This is all refactorable, so if there’s a better way to approach the situation overall, let me know. This got wordy really quick, sorry.


I’m creating a movie database, and I’ve got 3 tables, Movies, Genres and MovieToGenre. I’ve created the Genre table, and to populate the MtG table, I’m trying to associate a genre_id with a movie_id, which is a one-to-many relationship, I think, so it’ll have something like the following in it

movie_id | genre_id
1    56
1    786
2    232
2    656

But my issue is that in order to fill that table, I need the movie_id from the Movie model. I assumed that once I’ve added the Movie to the Movies table, that the model would retroactively update its movie_id property, but that doesn’t seem to be the case, as the MovieToGenre table gets populated with movie_id‘s equalling 0, the default value. The Movies table is using the proper movie_id so it’s a matter of telling my FillMovieToGenre method which movie has what movie_id.

The general process I’m using is the following loop:

  1. Create Movie instance, not setting the movie_id, since MVC
    chooses one itself

    a. movie = new Movie(data1, data2)

  2. Add Movie instance to table

    a. db.Movies.Add(movie)

  3. Use the last Movie instance’s movie_id in a
    FillMovieToGenreTable() method.

    a. FillMtGTable(movie) But at this point, the movie does not contain the non-default movie_id and so the MtG table is filled with movie_id = 0, where it should be movie_id = 1231 etc.

  4. GOTO 1.

tl;dr Possible to get a primary key for a recently added model? Seems like making a new query would be a bad idea, since I’m looping over all the movies I need to add, and that new query would slow the process down, I’d think.

edited in the relevant parts of the Movie class

public class Movie
{
    [Key]
    public int movie_ID { get; set; }

    [DisplayName("Title")]
    public string title { get; set; }
 }

public class Genre
{
    [DisplayName("Genre ID")]
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int genre_ID { get; set; }

    [DisplayName("Genre")]
    public string genre_string { get; set; }
}

public class MovieToGenre
{

[Key]
    public int movie_to_genre_ID { get; set; }

    public virtual int movie_ID { get; set; }

    public virtual int genre_ID { get; set; }
}
  • 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-06-15T16:21:07+00:00Added an answer on June 15, 2026 at 4:21 pm

    In EF after you save changes in other words after you run

    db.Movies.Add(movie)
    db.SaveChanges();
    

    EF should automatically give you the populated ID

    int movie_id=movie.movie_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So maybe I have this all wrong, but I'm sure there is a way
Maybe I'm going about this all the wrong way... But what I'm trying to
I come up against this all the time, and I'm never sure which way
Let's say I have model like this (all irrelevant fields removed for clarity): A(Model):
Well i been fighting with this all yesterday and today, i got a simple
HI all, I might be going about this all the wrong way, so any
This all, in my project I need to know the name of my view
This all seems to work great just the really strange thing is when I
This all got a little trickier than I had intended. I'm using the HistoricalReportWrapper
I've being trying this all night through. Is there any good solution to do

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.