CustomTypeHandler似乎无法正常工作

时间:2018-02-05 12:48:43

标签: c# dapper typehandler dapper-fastcrud

我有一个这样的实体:

[Table("Entity", Schema = "dbo")]
internal class Entity
{
    [Key]
    public virtual int Id { get; set; }
    public virtual CustomType Filters { get; set; }
    public virtual string Name { get; set; }
    ...
}

和这样的自定义类型:

[JsonObject]
public class CustomType
{
    [JsonProperty(PropertyName = "a")]public string a{ get; set; }
    [JsonProperty(PropertyName = "b")]public string b { get; set; }
    [JsonProperty(PropertyName = "d")]public IReadOnlyCollection<AnotherType> d{ get; set; }
}

这样的自定义处理程序:

public class JsonTypeHandler<CustomType> : SqlMapper.TypeHandler<CustomType>
{
    public override void SetValue(IDbDataParameter parameter, CustomType value)
    {
        parameter.Value = value == null
            ? (object) DBNull.Value
            : JsonConvert.SerializeObject(value);
        parameter.DbType = DbType.String;
    }

    public override CustomType Parse(object value)
    {
        return JsonConvert.DeserializeObject<CustomType>(value.ToString());
    }
}

和我register在global.asax.cs中这样:

SqlMapper.AddTypeHandler(new JsonTypeHandler<CustomType>());

问题是调用Execute时不会调用SetValue。我正在使用Dapper.FastCrud,我向db插入实体的代码如下所示:

var entity = new Entity
{
    Filters = new CustomType {...},
    Name = "fafsd",
    ...
};

_dbConnection.Insert(dataExtractEntry, x => x.AttachToTransaction(tx));

我还有什么需要做的,或者我错过了什么?

0 个答案:

没有答案