Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having 2 table studentmaster and subject they have many 2 many relationship :-

My table structure is :-
XML
How can i insert data in these table from mvc controller

This is studentmaster.cs

public partial class studentmaster
    {
        public studentmaster()
        {
            this.subjects = new HashSet<subject>();
        }

    public int studentid { get; set; }
    public string studentname { get; set; }
    public Nullable<System.DateTime> dob { get; set; }
    public Nullable<int> registrationnumber { get; set; }

    public virtual ICollection<subject> subjects { get; set; }
}
This is subject.cs

using System; using System.Collections.Generic;

public partial class subject
{
    public subject()
    {
        this.studentmasters = new HashSet<studentmaster>();
    }

    public int subjectid { get; set; }
    public string subjectname { get; set; }

    public virtual ICollection<studentmaster> studentmasters { get; set; }
}
This Model3.context.cs

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

public partial class studentEntities : DbContext
{
    public studentEntities() : base("name=studentEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<studentmaster> studentmasters { get; set; }
    public DbSet<subject> subjects { get; set; }
    public DbSet<sysdiagram> sysdiagrams { get; set; }
}
Posted
Comments
[no name] 18-Apr-14 12:56pm    
Is there any solution availaible to this? ....i have posted this question two times but no relevant answer plz help help i am in trouble
Kschuler 18-Apr-14 17:21pm    
Why the many to many relationship? That means you MUST have a student in order to have a Subject exist in your system. I think you should have 3 tables. Student, Subject, and StudentSubject. The Student file would just have Student ID. The Subject file would just have Subject ID. The StudentSubject file would have Student ID and Subject ID. This allows lists of Students and lists of Subjects to be edited independently.

1 solution

As pointed out correctly by Kschuler you should have 3 tables Student,Subject and StudentSubject.
EF doesn't generate StudentSubject table as for EF they implement many to many relationship by using navigation property unlike Database where you need to add another table.

C#
public static void InsertDAL()
       {
           using (var db= new MeenaEntities())
           {
              //Add sample Students data
               db.Students.Add(new Student { StudentId = 1, StudentName = "StudentName1", Dob = Convert.ToDateTime("01-Apr-1990"), RegistrationNumber = 1111 });
               db.Students.Add(new Student { StudentId = 2, StudentName = "StudentName2", Dob = Convert.ToDateTime("01-Apr-1991"), RegistrationNumber = 1234 });
               //Add sample Subject data
               var subject1=new Subject { SubjectId = 1, SubjectName = "Subject1" };
               var subject2 = new Subject { SubjectId = 2, SubjectName = "Subject2" };
               var subject3 = new Subject { SubjectId = 3, SubjectName = "Subject3" };
               var subject4 = new Subject { SubjectId = 4, SubjectName = "Subject4" };
               db.Subjects.Add(subject1);
               db.Subjects.Add(subject2);
               db.Subjects.Add(subject3);
               db.Subjects.Add(subject4);
               db.SaveChanges();
               // now if you want to insert subject 1 and subject 3 for student 1 into StudentSubject Table
               var student1 = db.Students.Where(w => w.StudentId == 1).FirstOrDefault();

               student1.Subjects.Add(subject1);
               student1.Subjects.Add(subject3);
               db.SaveChanges();

           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900