title: Save a Dictionary as JSON in SQL with Dapper
body: <p>I need to save a Dictionary into my table with Dapper.  I have gleaned from other posts that I need to use the SqlMapper stuff now available in Dapper. </p>
  
  <p>In my example here I have a property called ExtendedProperties which I wish to serialize to JSON and place in the ExtendedProperties column of my table.  Here is the code I am using to attempt this.  Note that this code works for hydrating on the way out, but fails on an insert with the error:</p>
  
  <blockquote>
    <p>No mapping exists from object type
    System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib,
    Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib,
    Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to
    a known managed provider native type.'.</p>
  </blockquote>
  
  <pre><code>public class ExtendableObject
  {
      public ExtendableObject()
      {
          ExtendedProperties = new Dictionary&lt;string, object&gt;();
      }
  
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public Dictionary&lt;string, object&gt; ExtendedProperties { get; set; }
  
      private string ExtendedPropertiesJson
      {
          get
          {
              return JsonConvert.SerializeObject(ExtendedProperties);
          }
          set
          {
              ExtendedProperties = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(value);
          }
      }
  }
  
  public class ExtendableObjectTypeMap : SqlMapper.ITypeMap
  {
      private readonly SqlMapper.ITypeMap _original;
  
      public ExtendableObjectTypeMap(SqlMapper.ITypeMap original)
      {
          _original = original;
      }
  
      public ConstructorInfo FindConstructor(string[] names, Type[] types)
      {
          return _original.FindConstructor(names, types);
      }
  
      public SqlMapper.IMemberMap GetConstructorParameter(System.Reflection.ConstructorInfo constructor, string columnName)
      {
          return _original.GetConstructorParameter(constructor, columnName);
      }
  
      public SqlMapper.IMemberMap GetMember(string columnName)
      {
          if (columnName == "ExtendedProperties")
          {
              return new ExtendedPropertiesMemberMap();
          }
          else
          {
              return _original.GetMember(columnName);
          }
      }
  
      /// &lt;summary&gt;
      /// A mapper that finds a private member to do the serialization and hydration
      /// &lt;/summary&gt;
      public class ExtendedPropertiesMemberMap : SqlMapper.IMemberMap
      {
          public string ColumnName
          {
              get { return "ExtendedProperties"; }
          }
  
          public FieldInfo Field
          {
              get { return typeof(ExtendableObject).GetMember("ExtendedPropertiesJson", BindingFlags.Instance | BindingFlags.NonPublic).Single() as FieldInfo; }
          }
  
          public Type MemberType
          {
              get { return typeof(string); }
          }
  
          public ParameterInfo Parameter
          {
              get 
              { 
                  return null; 
              }
          }
  
          public PropertyInfo Property
          {
              get { return typeof(ExtendableObject).GetMember("ExtendedPropertiesJson", BindingFlags.Instance | BindingFlags.NonPublic).Single() as PropertyInfo; }
          }
      }
  
  SqlMapper.SetTypeMap(typeof(ExtendableObject), new ExtendableObjectTypeMap(SqlMapper.GetTypeMap(typeof(ExtendableObject))));
  </code></pre>