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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:34:27+00:00 2026-06-01T06:34:27+00:00

(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually

  • 0

(USING ENTITY FRAMEWORK 4.3 CODE FIRST)

The code is below. I want to actually be able to do something like:

var instanceStudent = new Student2(){...};
var instanceCourse = new Course2(){...};
instanceStudent.Add(instanceCourse);
db.SaveChanges();

…or anything like that, so long as it works. Basically, I currently am a noob at Entity Framework and I want to be able to know how to add an Entity within another Entity while the Junction table has data in it (i know it’s easy on an empty junction table, but i’m stumped on this one)

    public class Student2
    {
        [Key]
        public virtual int StudentId { get; set; }
        public virtual string StudentName { get; set; }

        public virtual ICollection<Enrollment2> Enrollments { get; set; }
    }

    public class Course2
    {
        [Key]
        public virtual int CourseId { get; set; }
        public virtual string CourseName { get; set; }

        public virtual ICollection<Enrollment2> Enrollments { get; set; }
    }

    public class Enrollment2
    {
        public virtual int StudentId { get; set; }
        public virtual int CourseId { get; set; }
        public virtual string Grade { get; set; }

        public virtual Student2 Student { get; set; }
        public virtual Course2 Course { get; set; }
    }

    public class ManyMany2 : DbContext, IContext
    {
        public DbSet<Student2> Students { get; set; }
        public DbSet<Course2> Courses { get; set; }
        public DbSet<Enrollment2> Enrollments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student2>()
                .HasKey(student => student.StudentId);
            modelBuilder.Entity<Course2>()
                .HasKey(course => course.CourseId);
            modelBuilder.Entity<Enrollment2>()
                .HasKey(enrollment => new { enrollment.StudentId, enrollment.CourseId });

            modelBuilder.Entity<Student2>()
                .HasMany(student => student.Enrollments)
                .WithRequired()
                .HasForeignKey(enrollment => enrollment.StudentId);
            modelBuilder.Entity<Course2>()
                .HasMany(course => course.Enrollments)
                .WithRequired()
                .HasForeignKey(enrollment => enrollment.CourseId);
        }

        public void Run()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<ManyMany2>());

            var c1 = new Course2() { CourseName = "Spanish" };
            var c2 = new Course2() { CourseName = "Science" };
            var c3 = new Course2() { CourseName = "History" };

            var s1 = new Student2() { StudentName = "JC" };
            var s2 = new Student2() { StudentName = "Joe" };
            var s3 = new Student2() { StudentName = "Jill" };
        }
    }
  • 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-01T06:34:28+00:00Added an answer on June 1, 2026 at 6:34 am

    Entity Framework does not support direct many-to-many associations if the join table has a payload. This means you have to do something like this:

    var instanceStudent = new Student2(){...};     
    var instanceCourse = new Course2(){...};
    var instanceEnrollment = new Enrollment2() 
          { Course = instanceCourse, Student = instanceStudent };
    db.Enrollments.Add(instanceEnrollment);
    db.SaveChanges(); 
    

    You could also do (or similar with Courses) the following:

    var instanceStudent = new Student2(){...};
    var instanceCourse = new Course2(){...};
    instanceStudent.Enrollments = new Enrollment2() { Course = instanceCourse };
    db.Students.Add(instanceStudent);
    db.SaveChanges(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone my problem is very simple. Using entity framework code first I want
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I am using Entity Framework Code First. I want to query entites from database
Using Entity Framework 4.1 code first and POCO entities in MVC3; I would like
I am using the entity framework (code first). I would like to know if
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I need to map a many-to-many relationship using Entity Framework Code First. Its a
I'm using Entity Framework 4.3.1 Code-First and I need to split an entity between
I'm creating a application using entity framework code-first and i'm facing some problems with
I have been using Entity Framework CTP with Code-First as in this tutorial by

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.