title: Asp.Net MVC 4 - Adding an object model to an ICollection within another object model
body: <h2>Some background</h2>
  
  <p>I have 2 tables in my Asp.Net MVC 4 Database first application, Submission and Comment. <em>(There are a great deal more but they are irrelevant for this question)</em>. Any given Submission can have any number of Comments (One to many) associated to it.</p>
  
  <p>I have a submission details page, on which a user can open a jQuery dialog box with a partial view inside it to add a comment to the submission. This partial view is bound to a strongly typed viewmodel created to add comments.</p>
  
  <h2>Tables/Models</h2>
  
  <p><strong>Submission Table</strong></p>
  
  <p>Here is the class model for the Submission table. (I have omitted some properties for simplicity).</p>
  
  <pre><code>Partial Public Class Submission
      Public Property Submission_ID As Integer
      Public Property Submission_Hash As String
      Public Property Created As Nullable(Of Date)
      Public Property Modified As Date
  
      Public Overridable Property Comments As ICollection(Of Comment) = New HashSet(Of Comment)
  End Class
  </code></pre>
  
  <p><strong>Comment Table</strong></p>
  
  <p>And here is the class model for the Comment table. (Again, I have omitted some properties for simplicity).</p>
  
  <pre><code>Partial Public Class Comment
      Public Property Comment_ID As Integer
      Public Property User_ID As Nullable(Of Integer)
      Public Property Comment_Type As Nullable(Of Integer)
      Public Property Comment1 As String
      Public Property Created As Nullable(Of Date)
      Public Property Modified As Date
  
      Public Overridable Property Comment_Type1 As Comment_Type
      Public Overridable Property User As User
      Public Overridable Property Submissions As ICollection(Of Submission) = New HashSet(Of Submission)
  End Class
  </code></pre>
  
  <h2>The Requirement</h2>
  
  <p>What I am trying to do, is add a new comment to the collection of comments that already exist for a given submission.</p>
  
  <h2>So far...</h2>
  
  <p>So far, I have a view model for the add comment dialog box which gets initialised in my controller and then returns the populated view model back to controllers post method. Here is the code</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>Public Class SubmissionAddCommentViewModel
  
      &lt;Display(Name:="Submission")&gt; _
      Public Property Submission_ID As Integer
  
      &lt;Display(Name:="User")&gt; _
      Public Property User_ID As Integer
  
      &lt;Display(Name:="Comment type")&gt; _
      Public Property Comment_Type As Integer
  
      &lt;Required(ErrorMessage:="Please enter a comment.")&gt; _
      &lt;Display(Name:="Comment")&gt; _
      Public Property Comment As String
  
  End Class
  </code></pre>
  
  <p><strong>ViewModel Builder</strong></p>
  
  <pre><code>Public Class SubmissionAddCommentViewModel_Builder
  
      Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment)
      ReadOnly db As GeosightEntities
      ReadOnly submission As Submission
  
      Public Sub New(db As GeosightEntities, submission As Submission)
          Me.db = db
          Me.submission = submission
      End Sub
  
      Public Function CreateFrom(entity As Comment) As SubmissionAddCommentViewModel Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment).CreateFrom
          Dim model = New SubmissionAddCommentViewModel()
  
          model.Submission_ID = submission.Submission_ID
          model.User_ID = GetLoggedIn_ID()
  
          Return model
      End Function
  
      Public Function Rebuild(model As SubmissionAddCommentViewModel) As SubmissionAddCommentViewModel Implements IModelBuilder(Of SubmissionAddCommentViewModel, Comment).Rebuild
          Return model
      End Function
  
      Public Sub Add(model As SubmissionAddCommentViewModel)
          Dim comment As New Comment
  
          ' Map the ViewModel and Model fields
          comment.Comment_Type1 = If(IsNothing(model.Comment_Type), Nothing, db.Comment_Type.Find(model.Comment_Type))
          comment.User = db.Users.Find(model.User_ID)
          comment.Comment1 = model.Comment
          comment.Modified = Now
  
          ' Add the comment
          db.Submissions.Find(model.Submission_ID).Comments.Add(comment)
          db.SaveChanges()
      End Sub
  
  End Class
  </code></pre>
  
  <p><strong>Controller Get Method</strong></p>
  
  <pre><code>Function AddComment(Optional ByVal id As Integer = Nothing) As ActionResult
  
      Dim commentAddView As New SubmissionAddCommentViewModel
      Dim submission As Submission = db.Submissions.Find(id)
      Dim comment As New Comment
  
      If IsNothing(submission) Then
          Return HttpNotFound()
      End If
  
      Me.builder_AddComment = New SubmissionAddCommentViewModel_Builder(db, submission)
  
      ' Create the instance of the submission add comment view model
      commentAddView = builder_AddComment.CreateFrom(comment)
  
      If Request.IsAjaxRequest Then
          Return PartialView("AddCommentPartial", commentAddView)
      End If
  
      Return View(commentAddView)
  End Function
  </code></pre>
  
  <p><strong>Controller Post method</strong></p>
  
  <pre><code>&lt;HttpPost()&gt; _
  Function AddComment(ByVal commentAddView As SubmissionAddCommentViewModel) As ActionResult
  
      Dim comment As New Comment
      builder_AddComment = New SubmissionAddCommentViewModel_Builder(db, db.Submissions.Find(commentAddView.Submission_ID))
  
      ' Handle invalid comment models
      If Not ModelState.IsValid Then
  
          If Request.IsAjaxRequest Then
              Return PartialView("AddCommentPartial", commentAddView)
          End If
  
          Return View(commentAddView)
      End If
  
      ' Add the comment
      Me.builder_AddComment.Add(commentAddView)
  
      ' Display the successful message
      If Request.IsAjaxRequest Then
          Return PartialView("AddCommentSuccessPartial", commentAddView)
      End If
  
      Return RedirectToAction("Details", New With {.id = commentAddView.Submission_ID})
  
  End Function
  </code></pre>
  
  <h2>Question</h2>
  
  <p>When testing the above functionality, execution is successful all the way through to <code>db.SaveChanges()</code> in the ViewModel builder, however execution just stops once this line has been run. I have no idea what is happening and as a result am completely stuck.</p>
  
  <p>Does anyone know whether <strong>A)</strong> I am going about adding the comment the right way? <strong>B)</strong> Why this line is stopping execution without any kind of error?</p>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>Thank you to Peter Smith for his suggestion of putting a Try Catch around the code, I dont know why I didn't think of or do that in the first place.</p>
  
  <p>I am now getting the follwing error:</p>
  
  <blockquote>
    <p>{MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT" &amp; vbLf &amp; "      <code>Submission_Comment</code>.<code>Submission_ID</code>, " &amp; vbLf &amp; "      <code>Submission_Comment</code>.' at line 1" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int64&amp; insertedId)" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp; affectedRows, Int64&amp; insertedId)" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()" &amp; vbCrLf &amp; "   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()" &amp; vbCrLf &amp; "   at MySql.Data.Entity.EFMySqlCommand.ExecuteNonQuery()" &amp; vbCrLf &amp; "   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary<code>2 identifierValues, List</code>1 generatedValues)" &amp; vbCrLf &amp; "   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)}</p>
  </blockquote>