abp icon indicating copy to clipboard operation
abp copied to clipboard

Abp Dapper Error parsing ExtraProperties

Open LordOfCinder2000 opened this issue 3 years ago • 1 comments

I received this error when using Dapper. My code: "(dbConnection.QueryAsync("select * from AbpUsers", transaction: await GetDbTransactionAsync())).ToList()" => Error:

  • System.Data.DataException: 'Error parsing column 2 (ExtraProperties={} - String)'

  • InvalidCastException: Invalid cast from 'System.String' to 'Volo.Abp.Data.ExtraPropertyDictionary'.

When I select only column "ExtraProperties" from table "IdentityUser" is work fine. My code: "(dbConnection.QueryAsync("select ExtraProperties from AbpUsers", transaction: await GetDbTransactionAsync())).ToList()"

LordOfCinder2000 avatar Dec 11 '22 15:12 LordOfCinder2000

hi

You can consider using CustomPropertyTypeMap. This is more related to Dapper.

https://github.com/DapperLib/Dapper/blob/6ec3804f2c44f2bf6b757dc3522bf009cc64b27d/tests/Dapper.Tests/TypeHandlerTests.cs#L59

maliming avatar Dec 12 '22 01:12 maliming

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 18 '23 02:02 stale[bot]

@LordOfCinder2000 I had the same issue, how are you able to deal with this? Did you got the optimal solution of the problem?

This is more related to Dapper.

Considering Dapper for their product and having ExtraProperties as one of their features, this should be more part or related of the ABP Framework itself.

https://github.com/DapperLib/Dapper/blob/6ec3804f2c44f2bf6b757dc3522bf009cc64b27d/tests/Dapper.Tests/TypeHandlerTests.cs#L59

This seems to be not the solution, var map is not even used in the test.

My solution is a bit of brute and there might be a more practical and optimal way to solve this. What i had to do with this is:

var result = (
    await (
        dbConnection.QueryAsync<IdentityUserView>(
            // Note: manually select properties here and rename ExtraProperties as ExtraProperties_String
            "select *, "ExtraProperties" as "ExtraProperties_String" from AbpUsers",
            transaction: await GetDbTransactionAsync()
        )
    )
).ToList();
result.ForEach(item =>
{
    var extraProperties = JsonSerializer.Deserialize<ExtraPropertyDictionary>(
        item.ExtraProperties_String
    );
    foreach (var kvp in extraProperties)
    {
        item.SetProperty(kvp.Key, kvp.Value);
    }
});
return new List<IdentityUser>(result);

public class IdentityUserView : IdentityUser
{
    public string ExtraProperties_String { get; set; }
}

jeffbuot avatar Feb 09 '24 05:02 jeffbuot