querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

Attribute map not working

Open hengway opened this issue 2 years ago • 3 comments

By refering to

https://github.com/sqlkata/querybuilder/issues/261 and https://github.com/sqlkata/querybuilder/issues/212 I tried to implement the attribute map in my model

using SqlKata;

namespace PrismDemo.Models
{
    public class User
    {
        [Ignore]
        [Column("user_id")]
        public int Id { get; set; }
        [Column("username")]
        public string Name { get; set; }
        [Column("password")]
        public string Pass { get; set; }
        [Column("user_first_name")]
        public string FirstName { get; set; }
        [Column("user_last_name")]
        public string LastName { get; set; }
        [Column("user_gender")] 
        public int Genders { get; set; }
        public User()
        {
        }
    }
}

but it look like doesn't work, below is the break point captured 2021-07-10 16_16_03-PrismDemo (Debugging) - Microsoft Visual Studio

I did put in this code: Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

After several tried, found it only work, if attribute and field name are same, example user_first_name to UserFirstName

Please help advice did I made a mistake which cause it not working?

Thank you

hengway avatar Jul 10 '21 08:07 hengway

I just discovered the same issue.

denisulmer avatar Jul 13 '21 16:07 denisulmer

One option is set a custom mapper.

public static PropertyInfo CustomPropertyMapper(Type type, string columnName) { //find and return the property with property name or column name }

SqlMapper.TypeMapProvider = (Type type) => new CustomPropertyTypeMap(type, CustomPropertyMapper);

nameel avatar Jul 16 '21 12:07 nameel

It's not working because SqlKata attributes eg. "Column" are used only for SqlKata.Execution Insert/Update. For now you have to use additional standard-dapper mapping methods.

jposert avatar Oct 26 '21 09:10 jposert