title: Save entity with relationship in Entity Framework / ASP.NET MVC 2
body: <p>I have been dealing with this for at least two days now. I don't have a book available to reference, and I cannot for the life of me find an explanation of how this is supposed to work.</p>
  
  <p>What I am trying to do is a simple operation:</p>
  
  <ol>
  <li>Load a create form populated with 3 dropdownlist elements which reference other tables</li>
  <li>Fill out the form</li>
  <li>Submit the form and save the entity</li>
  </ol>
  
  <p>I have tried every way I can think of to get this to work. This should be easy. I have tried it with and without a ViewModel. I think it's clearer without the ViewModel</p>
  
  <p><em>Note: Due to requirements, I am using .NET 3.5. Does this mean I am on an older version of EF?</em></p>
  
  <p><em>Another Note: Yes, I am using VB. No, that is not my choice.</em></p>
  
  <p>Controller:</p>
  
  <pre><code>    Function Create() As ActionResult
          PopulateForm()
          Return View()
      End Function
  
      &lt;HttpPost()&gt;
      Function Create(ByVal escalation As Escalation) As ActionResult
          If TryUpdateModel(escalation) Then
              repo.AddEscalation(escalation)
              repo.Save()
              Return RedirectToAction("Details", New With {.Id = escalation.Id})
          End If
  
          'The model did not save - there are validation errors'
          PopulateForm()
          Return View()
      End Function
  
      Private Sub PopulateForm()
          ViewData("categoryList") = repo.GetAllCategories().ToList()
          ViewData("statusList") = repo.GetAllStatuses().ToList()
          ViewData("pathList") = New List(Of Path)
      End Sub
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>Public Sub AddEscalation(ByVal esc As Escalation)
      esc.Created_At = DateTime.Now()
      entities.AddToEscalations(esc)
  End Sub
  
  Public Sub Save()
      entities.SaveChanges()
  End Sub
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>        &lt;div class="editor-label"&gt;
              &lt;%= Html.LabelFor(Function(model) model.Status)%&gt;
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
               &lt;%= Html.DropDownListFor(Function(model) model.Status, New SelectList(ViewData("statusList"), "Id", "Name"))%&gt;
               &lt;%= Html.ValidationMessageFor(Function(model) model.Status)%&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              &lt;%= Html.LabelFor(Function(model) model.Category)%&gt;
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%= Html.DropDownListFor(Function(model) model.Category, New SelectList(ViewData("categoryList"), "Id", "Name"))%&gt;
               &lt;%= Html.ValidationMessageFor(Function(model) model.Category)%&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>When using the DropDownListFor... model.Property, it fails in the TryUpdateModel call. The validation errors look like this in the returned form: <strong>The value '35' is invalid.</strong></p>
  
  <p>If I change it to DropDownListFor...model.Property.Id, it dies on SaveChanges() like so:</p>
  
  <pre><code>Cannot insert the value NULL into column 'Name', 
  table 'Escalations_Dev.dbo.Categories'; 
  column does not allow nulls. INSERT fails. 
  The statement has been terminated. 
  </code></pre>
  
  <p>In debugging, my Escalation does have a Category, with only the Id property populated and in the Detached state.</p>
  
  <p>If I do the following to populate the full objects:</p>
  
  <pre><code>Public Sub AddEscalation(ByRef esc As Escalation)
      esc.Created_At = DateTime.Now()
      esc.Category = Me.GetCategory(esc.Category.Id)
      esc.Status = Me.GetStatus(esc.Status.Id)
      esc.Path = Me.GetPath(esc.Path.Id)
      entities.AddToEscalations(esc)
  End Sub
  
  Public Function GetPath(ByVal id As Integer) As Path
      Dim path As Path = entities.Paths.FirstOrDefault(Function(c) c.Id = id)
      path.CategoryReference.Load()
      Return path
  End Function
  </code></pre>
  
  <p>I get: Entities in 'Escalations_Conn.Paths' participate in the 'FK_Paths_Categories' relationship. 0 related 'Category' were found. 1 'Category' is expected. </p>
  
  <p>I really appreciate any help anyone has.</p>