使用Dapper将字典保存为SQL中的JSON

时间:2014-06-17 15:59:05

标签: c# json dictionary dapper

我需要使用Dapper将Dictionary保存到我的表中。我从其他帖子中收集到了我需要使用Dapper现在提供的SqlMapper内容。

在我的示例中,我有一个名为ExtendedProperties的属性,我希望将其序列化为JSON并放在我的表的ExtendedProperties列中。这是我用来尝试这个的代码。请注意,此代码适用于在出路时保湿,但在插入时出现错误:

  

对象类型不存在映射   System.Collections.Generic.KeyValuePair`2 [[System.String,mscorlib,   版本= 4.0.0.0,文化=中立,   PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]到   已知的托管提供程序本机类型。'。

public class ExtendableObject
{
    public ExtendableObject()
    {
        ExtendedProperties = new Dictionary<string, object>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Dictionary<string, object> ExtendedProperties { get; set; }

    private string ExtendedPropertiesJson
    {
        get
        {
            return JsonConvert.SerializeObject(ExtendedProperties);
        }
        set
        {
            ExtendedProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(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);
        }
    }

    /// <summary>
    /// A mapper that finds a private member to do the serialization and hydration
    /// </summary>
    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))));

0 个答案:

没有答案