title: How to add record to multiple tables in MVC through EF Repositry
body: <p>I have three models like below
  First Student Model</p>
  
  <pre><code>public class Student
  {
      public int ID { get; set; }
  
      [ForeignKey("account")]
      public int AccountID { get; set; }
      public Account account{ get; set; }  
      //some more
  
  }
  </code></pre>
  
  <p>Second Class Model</p>
  
  <pre><code>public class ClassMigration
  {
      public int ID { get; set; }
      public String Name { get; set; }
  
      public bool A { get; set; }
      //etc
  }
  </code></pre>
  
  <p>And the third one is Student Class</p>
  
  <pre><code>public class StudentClass
      {
          public int ID { get; set; }
  
          [ForeignKey("student")]
          public int StudentID { get; set; }
          public Student student { get; set; }
  
          [ForeignKey("clas")]
          public int ClassID { get; set; }
          public ClassMigration clas { get; set; }
  
          public String Section { get; set; }
  
          public String Session { get; set; }
      }
  </code></pre>
  
  <p>And here are my abstract classes for Student and Class</p>
  
  <pre><code>    namespace Domain.Abstract
  {
      public interface IStudent
      {
          IEnumerable&lt;Student&gt; Students { get; }
          Student SaveStudent(Student student);
          Student DeleteStudent(int ID);
      }
  }
  
  
  
  namespace Domain.Abstract
  {
      public interface IClassMigration
      {
          IEnumerable&lt;ClassMigration&gt; Classes{ get; }
          ClassMigration SaveClass(ClassMigration clas);
          ClassMigration DeleteClass(int ID);
      }
  }
  </code></pre>
  
  <p>And repository for both the classes are:</p>
  
  <pre><code>namespace Domain.Concret
  {
      public class EFStudentRepository:IStudent
      {
          public readonly DbAccess context = new DbAccess();
  
          public IEnumerable&lt;Entities.Student&gt; Students
          {
              get { return context.Students; }
          }
  
          public Student SaveStudent(Entities.Student student)
          {
              if (student.ID == 0)
              {
                  Account account = new Account();
                  account.UserName = student.RegistrationNo;
                  account.Password = "123456";
                  account.Role = UserRole.Student;
                  context.Accounts.Add(account);
                  context.SaveChanges();
                  student.AccountID = context.Accounts.Max(m =&gt; m.ID);
  
                  context.Students.Add(student);
              }
              else
              {
                  var org = context.Students.Find(student.ID);
                  if (org != null)
                  {
                      context.Entry(org).CurrentValues.SetValues(student);
                  }
              }
              context.SaveChanges();
              if(student.ID==0)
              {
                  student.ID = context.Students.Max(m =&gt; m.ID);
              }
              return student;
          }
  
          public Entities.Student DeleteStudent(int ID)
          {
              var std = context.Students.Find(ID);
              if (std != null)
              {
                  context.Students.Remove(std);
                  context.SaveChanges();
              }
              return std;
          }
      }
  }
  
  
  
  namespace Domain.Concret
  {
      public class EFClassRepository:IClassMigration
      {
          public DbAccess context = new DbAccess();
          public IEnumerable&lt;Entities.ClassMigration&gt; Classes
          {
              get { return context.Classes; }
          }
  
          public Entities.ClassMigration SaveClass(Entities.ClassMigration clas)
          {
              if (clas.ID == 0)
              {
  
                  context.Classes.Add(clas);
              }
              else
              {
                  var org = context.Classes.Find(clas.ID);
                  if (org != null)
                  {
                      context.Entry(org).CurrentValues.SetValues(clas);
                  }
              }
              context.SaveChanges();
              if (clas.ID == 0)
              {
                  clas.ID = context.Classes.Max(m =&gt; m.ID);
              }
              return clas;
          }
  
          public Entities.ClassMigration DeleteClass(int ID)
          {
              var clas = context.Classes.Find(ID);
              if (clas != null)
              {
                  context.Classes.Remove(clas);
                  context.SaveChanges();
              }
              return clas;
          }
      }
  }
  </code></pre>
  
  <p>and my view for class and section</p>
  
  <pre><code>&lt;div class="form-group"&gt;
                          &lt;label for="Class"&gt;Class&lt;/label&gt;
                          @Html.DropDownList("Class", @ViewBag.Classes as SelectList, new { @class = "form-control" })
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label for="Section"&gt;Select Section&lt;/label&gt;
                          &lt;select id="Section" class="form-control"&gt;
                              &lt;option selected="selected" value="A"&gt;A&lt;/option&gt;
                              &lt;option value="B"&gt;B&lt;/option&gt;
                              &lt;option value="C"&gt;C&lt;/option&gt;
                              &lt;option value="D"&gt;D&lt;/option&gt;
                              &lt;option value="E"&gt;E&lt;/option&gt;
                              &lt;option value="F"&gt;F&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label for="FatherName"&gt;Father Name&lt;/label&gt;
                          &lt;input type="text" class="form-control" id="FatherName" placeholder="Father Name" /&gt;
  </code></pre>
  
  <p>Now I want when we add student to database there I want to save to other data to Student table and class information to StudentClass table. how can i do it??? thanks in advance </p>