title: Add single entry to ICollection dynamically one at a time (MVC4 EF)
body: <p>I'm having a hard time wrapping my head around this issue: Is there a way to add a single entry to an ICollection? One at a time, persisting the data to the database for each entry added?</p>
  
  <p><strong>Here is my specific situation:</strong></p>
  
  <p>I have a Test Model:</p>
  
  <pre><code>public partial class Test
  {
      public Test()
      {
          this.Students = new HashSet&lt;Student&gt;();
          this.Questions = new HashSet&lt;Question&gt;();
      }
  
      public int Id { get; set; }
      public string name { get; set; }
      public string description { get; set; }
  
      public virtual ICollection&lt;Student&gt; Students { get; set; }
      public virtual ICollection&lt;Question&gt; Questions { get; set; }
  }
  </code></pre>
  
  <p>A Test contains a Collection of Questions. Question Model:</p>
  
  <pre><code>public partial class Question
  {
      public Question()
      {
          this.Answers = new HashSet&lt;Answer&gt;();
      }
  
      public int Id { get; set; }
      public string query { get; set; }
  
      public virtual Test Test { get; set; }
      public virtual ICollection&lt;Answer&gt; Answers { get; set; }
  }
  </code></pre>
  
  <p>My goal is for a user to have the ability to create a test, with a name and description, and then dynamically add questions to that test until they are satisfied. As of right now, creating the test is easy, but loading the test with questions is proving challenging for me. I had envisioned a form that would submit a Question and a Test.Id to a controller action which would persist the data to the database. But I don't understand how I can dynamically add a question to a ICollection of a Test. </p>
  
  <p>Is there a way to add a single entry to an ICollection? One at a time?</p>
  
  <p>One of the many attempts I've made</p>
  
  <p>Thank you so much for any help!</p>
  
  <p><strong>In response to levelnis:</strong> I tried this with no luck</p>
  
  <pre><code>    public void AddQuestionToTest(Test test, Question question)
      {
          question.Test = test;
          test.Questions.Add(question);
          Update(test);
          SaveChanges();
      }
  </code></pre>
  
  <p>It's not adding any questions to the db. Any ideas?</p>
  
  <p><strong>EDIT:</strong> The only code I've tried that gets a question into the database is:</p>
  
  <pre><code>public void AddQuestionToTest(Test test, Question question)
  {
      question.Test = test;
      Insert(question);
      SaveChanges();
  }
  </code></pre>
  
  <p>..but this makes another Test in the database with the same name/description but a new id that has been incremented.</p>