title: Is it possible to use a composite class of entities, updates those properties and then save the composite object to the DB?
body: <p>I would like to create a composite class with multiple properties which represent multiple entities. It is like a transaction class. I then update an instantiation of this class from my ViewModel in my controller. I would then like to invoke a "Save" method on this class to save any changes to the properties which are of type entity. I have tried it and it did work, but now will not work. I believe that I have lost "Context" since if I do it directly in the controller it does work(Option3 below).</p>
  
  <p>Here is some code of my class. It is a little contrived. It is the principle that I am interested in, ie using a document/transaction class to hold multiple entity data:</p>
  
  <pre><code>  public class OrderTransaction
  {
      public OrderEntities db = new OrderEntities();
  
      public int OrderId { get; set; }
  
      private entityOrder _myOrder;
      public entityOrder myOrder
      {
          get
          {
              return this._myOrder;
          }
          set
          {
              this._myOrder= value;
          }
      }
  
      private List&lt;entityOrderItem&gt; _myOrderItems;
      public List&lt;entityOrderItem&gt; myOrderItems
      {
          get
          {return this._myOrderItems;}
          set
          {this._myOrderItems = value;}
      }
  
      public Boolean Load(int OrderId = 1)
      {
          if (db.entityOrder.Where(o =&gt; o.OrderId == OrderId).Any())
          {
      myOrder = db.entityOrder.Where(o =&gt; o.OrderId == OrderId).First();
      }
          if (db.entityOrderItem.Where(oi =&gt; oi.OrderId == OrderId).Any())
          {
      myOrderItems = db.entityOrderItem.Where(oi =&gt; oi.OrderId == OrderId).ToList();
          }
  
          return true;
  
      }
  
  
      public Boolean Save()
      {
  
          if ((db.SaveChanges()) &gt; 0)
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>If this class does look fine, then I guess my question is why can I not get the save working from the controller</p>
  
  <pre><code>        [HttpPost]
      public ActionResult Edit(vmodel myVM)
      {
  </code></pre>
  
  <p>Option1 :Saving directly off the VM. I thought I had this working, but not now.</p>
  
  <pre><code>     vmodel.MyOrderTransaction.Save(); 
  </code></pre>
  
  <p>Option2 : Assigning to composite object using object mapping and saving</p>
  
  <pre><code>     OrderTransaction myOrderTransaction = new OrderTransaction();
       myOrderTransaction.injectFrom&lt;IgnorNulls&gt;myVM.MyOrderTransaction 
  </code></pre>
  
  <p>Option3 : Find specific object in current context and assigning values from VM and "SaveChanges()" directly.</p>
  
  <pre><code>     var myOrders = db.Orders.where(....
       myOrder.Name = "...
       db.DaveChanges();
  </code></pre>
  
  <p>Option3 does work, but I am not using my composite class with this approach.</p>
  
  <p>So how can I get my composite class approach to save for all singleton and list properties.</p>
  
  <p>Huge thanks in advance.</p>
  
  <p>EDIT</p>
  
  <p>Just found that if I add a contructor for "OrderTransaction" that retrieves in the record then Option1 will work ie straight off the VM/Model Binding. </p>
  
  <pre><code>        public OrderTransaction()
      {
  
          myOrder = db.Orders.Where(o =&gt; o.OrderId == 44).First();
  
      }
  </code></pre>
  
  <p>However I really need to pass in a parameter into the contructor ie ID, but am unsure how to do this if required.</p>
  
  <p>EDIT2:</p>
  
  <p>Actually Option2 would be best since I can always ensure that forms are not resetting values to null which the ViewModel may represent for some entities as they are not on that form. So one would retrieve the data set, map the form entities ignoring any nulls and then save this new object.</p>
  
  <pre><code>        OrderTransaction myOrderTransaction = new OrderTransaction(OrderID);
          myOrder.InjectFrom&lt;IgnoreNulls&gt;(MyVM.myFormOrder);
          myOrder.Save();
  </code></pre>
  
  <p>Unfortunately the above does not work as the context has changed from the ViewModel which is a shame. </p>