Skip to content

case sensitivity issue when mapping dynamic dapper objects #39

Open

Description

@iulian0512

Dapper creates a case sensitive dictionary when returning the query result and you make a simple casing mistake on a column name the slapper will not map.
To fix change line 134 in Slapper.AutoMapper.cs with:
var dictionary = dynamicListOfProperties .Select(dynamicItem => new Dictionary<string, object>(dynamicItem as IDictionary<string, object>,StringComparer.OrdinalIgnoreCase)).ToList();

To reproduce the issue run this simple example:

    public class book
    {
        [AutoMapper.Id]
        public string name { get; set; }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            var cnn = DBUtils.CreateConnection("Data Source=localhost;Integrated Security=true;Initial Catalog=local;", DbServType.SqlServer);
            cnn.Open();

            var qres= cnn.Query<dynamic>(
                @"select 'xxxx' as 'Name'
                UNION
                select 'asdas' as 'Name'");

            var ent = AutoMapper.MapDynamic<book>(qres,false);

        }
    }

Activity

  1. nathan-alden-sr commented on Aug 11, 2016

    @nathan-alden-sr

    I wish SlapperAutoMapper had better support for controlling case sensitivity and conversions of one name to another. I am using a PostgreSQL database that has column names like my_column (the standard naming convention for PostgreSQL databases) and I'm trying to map them to a C# property called MyColumn. The only way I've found to do this is by aliasing every column that contains an underscore within the SQL statement. Tedious, for sure.

    I also have similar problems mapping PostgreSQL enumeration values (my_enum_value) to a C# enumeration (EnumValues.MyEnumValue).

  2. Dolfik1 commented on Feb 17, 2017

    @Dolfik1

    Also faced with this problem. Simple code to reproduce the problem:

    
        public class TestModel
        {
            [Slapper.AutoMapper.Id]
            public int IdModel { get; set; }
    
            public string Text { get; set; }
        }
    
    ...
    
        var dictionary = new List<Dictionary<string, object>>
        {
            new Dictionary<string, object>
            {
                { "IDMODEL", 1 },
                { "TEXT", "TEXT1" }
            },
            new Dictionary<string, object>
            {
                { "IDMODEL", 2 },
                { "TEXT", "TEXT2" }
            }
        };
    
        var items = Slapper.AutoMapper.Map<MaterialGeneric>(dictionary);
        Console.WriteLine(items.Count() == 2);
    
    

    Output: False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions