title: How to update complex model in ASP.NET MVC 3
body: <p>I am trying to update a complex model in a single view.
  I am using ASP.NET MVC3, Entity Framework with Code first, unit of work, generic repository pattern..
  but when I try to update the model, i come up with this error:</p>
  
  <p><strong>A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.</strong></p>
  
  <p>Here is my simplified view model:</p>
  
  <pre><code>public class TransactionViewModel
  {
       public Transaction Transaction { get; set; }
       public bool IsUserSubmitting { get; set; }
       public IEnumerable&lt;SelectListItem&gt; ContractTypes { get; set; }
  }
  </code></pre>
  
  <p>Here is my simplified complex model, and as an example one of its navigation property. 
  Transaction has one to one relationship with all of its navigation properties:</p>
  
  <pre><code>public class Transaction
  {
      [Key]
      public int Id { get; set; }
  
      public int CurrentStageId { get; set; }
  
      public int? BidId { get; set; }
  
      public int? EvaluationId { get; set; }
  
      public virtual Stage CurrentStage { get; set; }
  
      public virtual Bid Bid { get; set; }
  
      public virtual Evaluation Evaluation { get; set; }
  
  }
  
  public class Bid
  {
      [Key]
      public int Id { get; set; }
  
      public string Type { get; set; }
  
      public DateTime? PublicationDate { get; set; }
  
      public DateTime? BidOpeningDate { get; set; }
  
      public DateTime? ServiceDate { get; set; }
  
      public string ContractBuyerComments { get; set; }
  
      public string BidNumber { get; set; }
  
      public DateTime? ReminderDate { get; set; }
  
      public DateTime? SubmitDate { get; set; }
  
  }
  </code></pre>
  
  <p>Using the same view model, I am able to create a transaction object, which would populate the database like this.</p>
  
  <p>Id: 1, CurrentStageId: 1, BidId: 1, EvaluationId: 1 </p>
  
  <p>but, when I try to update properties within these navigation properties, this line causes the error, in controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(TransactionViewModel model)
  {
      if (ModelState.IsValid)
      {
          -&gt; unitOfWork.TransactionRepository.Update(model.Transaction);
             unitOfWork.Save();
             return RedirectToAction("List");
      }
  }
  </code></pre>
  
  <p>In generic repository: </p>
  
  <pre><code>public virtual void Update(TEntity entityToUpdate)
  {
   -&gt; dbSet.Attach(entityToUpdate);
      context.Entry(entityToUpdate).State = EntityState.Modified;
  }
  </code></pre>
  
  <p><strong>The problem is further complicated because I should be able to edit any of the fields(properties) within any of the navigation property within Transaction object within a single view.</strong> </p>