Skip to content

Question: IPropertyHandler for dictionary<string,string> #647

@kbilsted

Description

@kbilsted

In .Net Core 3 when serializing a dictionary to a column I get the error

System.ArgumentException : No mapping exists from object type System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] to a known managed provider native type.

   at System.Data.SqlClient.MetaType.GetMetaTypeFromValue(Type dataType, Object value, Boolean inferLen, Boolean streamAllowed)

   at System.Data.SqlClient.MetaType.GetMetaTypeFromType(Type dataType)

   at System.Data.SqlClient.SqlParameter.GetMetaTypeOnly()

   at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)

   at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)

   at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)

   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)

   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)

   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)

   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

   at System.Data.Common.DbCommand.ExecuteReader()

   at RepoDb.DbConnectionExtension.ExecuteQueryInternal(IDbConnection connection, String commandText, Object param, Nullable`1 commandType, String cacheKey, Nullable`1 cacheItemExpiration, Nullable`1 commandTimeout, IDbTransaction transaction, ICache cache, String tableName, Boolean skipCommandArrayParametersCheck)

   at RepoDb.DbConnectionExtension.ExecuteQuery(IDbConnection connection, String commandText, Object param, Nullable`1 commandType, String cacheKey, Nullable`1 cacheItemExpiration, Nullable`1 commandTimeout, IDbTransaction transaction, ICache cache)

Using the following

    public class JsonObjectTypeHandler : IPropertyHandler<string, Dictionary<string, string>>
    {
        public Dictionary<string, string> Get(string input, ClassProperty property)
        {
            return JsonConvert.DeserializeObject<Dictionary<string, string>>(input);
        }
 

        public string Set(Dictionary<string, string> input, ClassProperty property)
        {
            return JsonConvert.SerializeObject(input);
        }
    }

and trying to register it both as RepoDb.PropertyHandlerMapper.Add<Dictionary<string, string>, JsonObjectTypeHandler>();

and on my model object

    public class Foo
    {
        public Guid Id { get; set; }

        [PropertyHandler(typeof(JsonObjectTypeHandler))] 
        public Dictionary<string, string> Bars { get; set; }
   }

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixedThe bug, issue, incident has been fixed.priorityTop priority feature or things to dotodoThings to be done in the future

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions