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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:10:39+00:00 2026-05-24T07:10:39+00:00

I been practicing with EF Code First, SQL Express, and ASP.Net MVC3. When I

  • 0

I been practicing with EF Code First, SQL Express, and ASP.Net MVC3.

When I run the website first the correct tables are generated by the FooInitializer and Student and Image are populated but for some reason the join table (StudentImages) is not being populated.

What could be the issue?

Tables: Student, Image, and StudentImages

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 string Extension { get; set; }

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

public class FooInitializer : DropCreateDatabaseIfModelChanges<DBContext>
{

    protected override void Seed(DBContext context)
    {
    var students = new List<Student> {
        new Student { Id = 1, Name = "John" },
        new Student { Id = 2, Name = "Jane" }
    };

    students.ForEach(s => context.Students.Add(s));
    context.SaveChanges();


    var images = new List<Image> {
    new Image { Id = 1, Filename = "IMG_4596.JPG", Extension = ".jpg" },
        new Image { Id = 2, Filename = "IMG_4600.JPG", Extension = ".jpg" }
    };

    images.ForEach(i => context.Images.Add(i));

    students[0].Images.Add(images[0]);
    context.SaveChanges();
    }
}
  • 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-24T07:10:40+00:00Added an answer on May 24, 2026 at 7:10 am

    From what I can tell your Image class does not have a reference to the StudentID. Try adding:

    public int StudentID { get; set; } 
    

    to the Image class maybe?

    Also having an ICollection would mean that one image could have multiple students – is this correct? Maybe it should be a public virtual Student Student {…}

    EDIT: Also I found this, with a many to many relationship (if thats what you need):

    In your OnModelCreating() Method:

    modelBuilder.Entity<Student>()
        .HasMany(c => c.Images).WithMany(i => i.Students)
        .Map(t => t.MapLeftKey("StudentId")
            .MapRightKey("ImageID")
            .ToTable("StudentImages"));
    

    taken from this link that states:

    A many-to-many relationship between the Instructor and Course
    entities. The code specifies the table and column names for the join
    table. Code First can configure the many-to-many relationship for you
    without this code, but if you don’t call it, you will get default
    names such as InstructorInstructorID for the InstructorID column.

    EDIT: Here is the code I used the other night, with my implementation of the code first MVC site:

    var users = new List<User> 
                {
                    new User { UserID = new Guid(), Email = "me@me.com", LastOnline = DateTime.Now, Password = "pword", RegistrationDate = DateTime.Now, SecurityAnswer = "me", SecurityQuestion = "who?", Roles = new List<Role>() },
                };
    
                users.ForEach(s => context.Users.Add(s));
                context.SaveChanges();
    
                var roles = new List<Role>
                {
                    new Role { RoleID = "Admin", Description = "Administration Users", Users = new List<User>() }
    
                };
                roles.ForEach(r => context.Roles.Add(r));
    
    
                users[0].Roles.Add(roles[0]);
                context.SaveChanges();
    
    
    
                var userLicense = new List<UserLicense>
                {
                    new UserLicense { AddDateTime = DateTime.Now, LicenseType = "Farmer", Manufacturer = "Motorola", Model = "Droid", PhoneIdentifier = "c0e4223a910f", UserID = users[0].UserID, User = new User() }
    
                };
                userLicense[0].User = users[0];
                userLicense.ForEach(u => context.UserLicenses.Add(u));
                context.SaveChanges();
    
                userLicense[0].User = users[0];
                context.SaveChanges();
    

    Notice in each instantiated item, I am also instantiating a new referenced item within the parent object.

    EDIT:

    Ok try this:

    var students = new List<Student> {
        new Student { Id = 1, Name = "John", Images = new List<Image>() },
        new Student { Id = 2, Name = "Jane", Images = new List<Image>() }
    };
    
    students.ForEach(s => context.Students.Add(s));
    context.SaveChanges();
    
    
    var images = new List<Image> {
        new Image { Id = 1, Filename = "IMG_4596.JPG", Extension = ".jpg", Students = new List<Student>()  },
        new Image { Id = 2, Filename = "IMG_4600.JPG", Extension = ".jpg", Students = new List<Student>() }
    };
    
    images.ForEach(i => context.Images.Add(i));
    
    students[0].Images.Add(images[0]);
    students[1].Images.Add(images[1]);
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been a while since I've dealt with ASP.NET and this is the first time
So I've been practicing my Java programming skills on the CodingBat website, when I
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
I'm trying to develop a c#/asp.net application to extract data from a SQL server
I have been practicing TDD and (some) XP for a few years now and
I have been practicing TDD in C# for several years now and recently have
I've been practicing for an upcoming programming competition and I have stumbled across a
I have been quite comfortable with windows-services, I have been practicing since from last
Been going over my predecessor's code and see usage of the request scope frequently.
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints

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.