Dapper自定义SqlMapper.TypeHandler解析方法未调用

时间:2019-01-21 14:55:09

标签: dapper asp.net-core-2.2

我创建了一个SqlMapper.TypeHandler,将Customer对象映射到CreditAccount类,如下所示:

public class CustomerTypeHandler : SqlMapper.TypeHandler<Customer>
{
    public override Customer Parse(object value)
    {
        throw new NotImplementedException();
    }
    public override void SetValue(IDbDataParameter parameter, Customer 
    value)
    {
        throw new NotImplementedException();
    }
}

public class CreditAccount
{
    public int AccountId { get; set; }
    public Customer Customer{ get; set; }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}

当我连接到数据库并调用一个sproc时,从不调用CustomerTypeHandler Parse方法,并且我的CreditAccount对象仅填充有AccountId。 Customer对象为空。

我这样称呼它:

    public async Task<CreditAccount> GetCreditAccount(int accountId)
    {
        var sql = "MY PROC NAME HERE";
        var parameters = new DynamicParameters();
        parameters.Add("@AccountId", accountId);

            SqlMapper.AddTypeHandler(new CustomerTypeHandler());

            using (IDbConnection connection = Connection)
            {
                connection.Open();
                var account = await connection.QueryFirstAsync<Check>(sql, parameters, commandType: CommandType.StoredProcedure);
                return account;
            }
        }
    }

我在Parse方法中放置了一个断点,但从未调用过它。 数据库连接正常,我正在获取AccountId。

我的环境; .NET Core 2.2
Dapper 1.50.5

代码很简单。我没有例外。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

一年过去了,现在Dapper 2.0.30中没有此错误。 我在Postgres的jsonb列上进行了检查。

using Dapper;
using Newtonsoft.Json;
using Npgsql;
using System;
using System.Data;

    public class CreditAccount
    {
        public int AccountId { get; set; }
        public Customer Customer { get; set; }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
    }

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

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

使用此类的示例-一切正常。

        static void Main(string[] args)
        {

            using (var connection = GetDefaultConnection())
            {
                connection.Open();
                var customer = new Customer
                {
                    FirstName = "Gaday",
                    LastName = "Ivanova",
                    MiddleName = "Petrovich"
                };
                var jsonData = JsonConvert.SerializeObject(customer);
                var strQuery = $"SELECT 10500 as AccountId,'{jsonData}'::jsonb as Customer";

                SqlMapper.AddTypeHandler(new CustomerJsonObjectTypeHandler());

                try
                {
                    var data = connection.QueryFirst<CreditAccount>(strQuery);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

        }