Skip to content

CustomTypeHandler seems to doesn't work correctly #934

@MNie

Description

@MNie

I have an entity like this:

[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; }
    ...
}

and custom type like this:

[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<d> d{ get; set; }
}

custom handler like this:

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());
    }
}

and I register it in global.asax.cs like this:

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

The problem is that SetValue is not invoked when Execute is called. I'm using a Dapper.FastCrud, and my code to insert an entity to db looks like this:

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

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

Is there something more I have to do here?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions