title: Insert to multiple foreign key tables in MVC with super-type/sub-type database-first model
body: <p>I have two sub-types of a super-type "Entity", namely "Household" and "Involved Body".</p>
  
  <p>I've modeled them as shown below in my database and they were auto-generated to the EF Model (again shown below).</p>
  
  <p><strong>database</strong>
  <img src="https://i.stack.imgur.com/NQ5v5.png" alt="enter image description here"></p>
  
  <p><strong>edmx model</strong>
  <img src="https://i.stack.imgur.com/OU832.png" alt="enter image description here"></p>
  
  <p>Using the default scaffolding for MVC I am able to add a new Household without any problems. However, when I try to add a new Involved Body I hit an error when it tries to add the Entity Type.</p>
  
  <p>There only relevant (as far as I can tell) difference between the two sub-types is that the EntityType for a Household is hard-coded as "Household" whereas the EntityType for an Involved Body can be any EntityType except "Household" - this is selected from a list by the user.</p>
  
  <p>The Create Action on the HTTP POST for the Involved Body throws an error relating to the foreign key between tEntity and tEntityType with the tEntityType being null. Code as follows:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create([Bind(Exclude = "entityID")]tEntity tentity            
                                                          , tInvolvedBody tinvolvedbody
                                                          , tAddress taddress
                                                          , tAddressEntity taddressentity
                                                          //, tEntityType tentitytype
                                                          , int entityTypeID
                                                          )
  {
  
      #region entity type
      //find entity type from id
      var tentitytype = db.tEntityTypes.Find(entityTypeID);                     
  
      #endregion
  
      #region address
      //assume start date of involved body not needed for reporting
      taddressentity.startDate = DateTime.Now.Date;
  
      #endregion
  
  
      if (ModelState.IsValid)
      {
  
          db.tEntities.Add(tentity);
          db.tInvolvedBodies.Add(tinvolvedbody);
          db.tAddresses.Add(taddress);
          db.tAddressEntities.Add(taddressentity);
  
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      //recreate viewbag for entityType dropdown
      var q = (
              from e in db.tEntityTypes
              where e.entityType != "Household"
              select e
          );
  
      ViewBag.entityTypeID = new SelectList(q, "entityTypeID", "entityType");
      return View(tinvolvedbody);
  }
  </code></pre>
  
  <p>I've tried adding the tEntityType to the parameters list for the create but this results in the ModelState.IsValid returning false due to the entityType being null on all the objects.</p>
  
  <p>I've also tried actively linking the entity type to each of the other objects using:</p>
  
  <pre><code>tentity.tEntityType = tentitytype;
  tinvolvedbody.tEntity.tEntityType = tentitytype;
  taddressentity.tEntity.tEntityType = tentitytype;
  </code></pre>
  
  <p>The above ends up working but it creates a new Entity for each of the other objects i.e. I get three new rows in my tEntity table, one is the Entity, one links to tInvolvedBody and one links to tAddressEntities. This makes no sense...</p>
  
  <p>How can I insert a new InvolvedBody that creates an Entity, picks up the Entity Type and then links to the AddressEntity junction table?</p>