title: Inserting and MultiMapping using Dapper.Contrib
body: <p>What I am attempting to do, is to be able to use complex objects both for retrieval and for writing back to the database.</p>
  
  <p>For example, I have two classes</p>
  
  <pre><code>[Table("Children")]
  public class Child
  {
      public int Id { get; set; }
      public int ParentId { get; set; }
      public string Data { get; set; }
  }
  
  [Table("Parents")]
  public class Parent
  {
      public int Id { get; set; }
      public string Data { get; set; }
      public List&lt;Child&gt; Children { get; set; }
  
      public Parent()
      {
          Children = new List&lt;Child&gt;();
      }
  }
  </code></pre>
  
  <p>I populate the Parent object using the method below</p>
  
  <pre><code>public List&lt;Parent&gt; GetAll()
  {
      var parents = new List&lt;Parent&gt;();
      using (SQLiteConnection conn = new SQLiteConnection(sqlConnectionString))
      {
          string sql = $@"
          SELECT * From Parents;
          SELECT * FROM Children;
          ;";
  
          var results = conn.QueryMultiple(sql);
          parents = results.Read&lt;Parent&gt;().ToList();
          var children = results.Read&lt;Child&gt;();
          foreach (var parent in parents)
          {
              parent.Children = children.Where(a =&gt; a.ParentId == parent.Id).ToList();
          }
      }
    return parents;
  }
  </code></pre>
  
  <p>My Question now that I have my populated object, say I want to make changes to Parent including adding/updating/remove values from Parent.Children</p>
  
  <p>This is my first pass at an update method, but I recognize there are issues here, such as making the determination to update or insert based on the child Id being zero or not, as well as this being a bit verbose.</p>
  
  <p><strong>Is there way, and am I missing some functionality of Dapper or Dapper.Contrib that would provide helper methods to make this process easier?</strong></p>
  
  <pre><code>public bool Update(Parent parent)
  {
      bool result = true;
      using (SQLiteConnection conn = new SQLiteConnection(sqlConnectionString))
      {
          conn.Open();
          using (var tran = conn.BeginTransaction())
          {
              try
              {
                  if (!conn.Update&lt;Parent&gt;(parent))
                  {
                      result = false;
                  }
  
                  foreach (var element in parent.Children)
                  {
                      if (element.Id == default(int))
                      {
                          conn.Insert&lt;Child&gt;(element);
                      }
                      else
                      {
                          if (!conn.Update&lt;Child&gt;(element))
                          {
                              result = false;
                          }
                      }
                  }
                  tran.Commit();
              }
              catch(Exception ex)
              {
                  //logger.Error(ex,"error attempting update");
                  tran.Rollback();
                  throw;
              }
          }
      }
  
      return result;
  }
  </code></pre>