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

  • Home
  • SEARCH
  • 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 6540961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:02:42+00:00 2026-05-25T11:02:42+00:00

I have two entities that have a relationship for which I create a join

  • 0

I have two entities that have a relationship for which I create a join table

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Image> Images { get; set; }
}


public class Image
{
    public int Id { get; set; }
    public string Filename { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

        modelBuilder.Entity<Student>()
            .HasMany(i => i.Images)
            .WithMany(s => s.Students)
            .Map(m => m.ToTable("StudentImages"));
}

I would like to add an additional column to allow chronological ordering of the StudentImages.

Where should I add insert the relevant code?

  • 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-25T11:02:42+00:00Added an answer on May 25, 2026 at 11:02 am

    Do you want to use that new column in your application? In such case you cannot do that with your model. Many-to-many relation works only if junction table doesn’t contain anything else than foreign keys to main tables. Once you add additional column exposed to your application, the junction table becomes entity as any other = you need third class. Your model should look like:

    public class StudentImage 
    {
        public int StudentId { get; set; }
        public int ImageId { get; set; }
        public int Order { get; set; }
        public virtual Student Student { get; set; }
        public virtual Image Image { get; set; }
    }
    
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<StudentImage> Images { get; set; }
    }
    
    
    public class Image
    {
        public int Id { get; set; }
        public string Filename { get; set; }
        public virtual ICollection<StudentImage> Students { get; set; }
    }
    

    And your mapping must change as well:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<StudentImages>().HasKey(si => new { si.StudentId, si.ImageId });
    
        // The rest should not be needed - it should be done by conventions
        modelBuilder.Entity<Student>()
                    .HasMany(s => s.Images)
                    .WithRequired(si => si.Student)
                    .HasForeignKey(si => si.StudentId); 
        modelBuilder.Entity<Image>()
                    .HasMany(s => s.Students)
                    .WithRequired(si => si.Image)
                    .HasForeignKey(si => si.ImageId); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have two entities which are Student and Class entities. Student and Class are
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have two entities, Class and Student , linked in a many-to-many relationship. When
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have created a core data model that has two entities which have a
I have two entities that have a many-to-many relationship; they are mapped with annotations
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have two entites, Person and Teacher , with a OneToMany relationship: public class
I have two entities: match and player , which I have set up with

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.