title: MVC3 binding nested model using Entity Framework with GUID keys
body: <p>I have been fighting with this all day and still I am failing.</p>
  
  <p>I can simplify the problem as follows:
  I have reports and reports have forms. I have entity models of each. They have Guid id's as shown below.</p>
  
  <p><img src="https://i.stack.imgur.com/jUgkR.png" alt="Model"></p>
  
  <p>I am trying to get a single view where I can create a report and a form. As an end goal I would like to be able to add multiple forms, but just one would be great. My controller is as follows: </p>
  
  <pre><code>    // GET: /AllInOne/Create
  
      public ActionResult Create()
      {
          ViewBag.PossibleReportBases = reportBaseRepository.All;
          ViewBag.PossibleCategories = categoryRepository.All;
  
          var model = new Report {FromDate = DateTime.Now};
          model.Forms.Add(new Form());
          return View(model);
      }
  
      // POST: /AllInOne/Create
  
      [HttpPost]
      public ActionResult Create(Report report)
      {
          if (ModelState.IsValid) {
              reportRepository.InsertOrUpdate(report);
              reportRepository.Save();
              return RedirectToAction("Index");
          }
          else
          {
              ViewBag.PossibleReportBases = reportBaseRepository.All;
              ViewBag.PossibleCategories = categoryRepository.All;
              return View();
          }
      }
  </code></pre>
  
  <p>The repository code looks like this:</p>
  
  <pre><code>    public void InsertOrUpdate(Report report)
      {
          if (report.Id == default(System.Guid)) {
              // New entity
              report.Id = Guid.NewGuid();
              context.Reports.AddObject(report);
          } else {
              // Existing entity
              context.Reports.Attach(report);
              context.ObjectStateManager.ChangeObjectState(report, EntityState.Modified);
          }
      }
  </code></pre>
  
  <p>At one stage the binding was giving me this error:
  The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph. </p>
  
  <p>I have tried many things for the views, but none of them have worked. </p>
  
  <p>Please help.</p>