henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
429 stars 88 forks source link

Mapping DB fields with spaces in their names #51

Closed fassergey closed 7 years ago

fassergey commented 7 years ago

Hi there. I tried to map with conventions fields which are returns by stored procedure. It was not successful. Please pay attention to this problem.

class Program
    {
        static void Main(string[] args)
        {
            FluentMapper.Initialize(config =>
            {
                // Configure all entities in the current assembly with an optional namespaces filter.
                config.AddConvention<ColumnMappingConvention>()
                      .ForEntitiesInCurrentAssembly();
            });

            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                var result = cnn.Query<HorseDiscrepancyDTO>("dbo.DiscrepancyTodayByHorseGet", new { horse_Id = 111111, Date = DateTimeOffset.UtcNow.AddDays(-1) }, commandType: CommandType.StoredProcedure).ToArray();
            }
        }
    }

public class ColumnMappingConvention : Convention
    {
        public ColumnMappingConvention()
        {
            Properties()
                .Configure(x => x.Transform(s => Regex.Replace(input: s, pattern: "Race_Name", replacement: "Race Name")));

            Properties()
                .Configure(x => x.Transform(s => s.Replace("1", "/")));            
        }
    }

    public class HorseDiscrepancyDTO
    {
        public DateTimeOffset? Race_Date1Time { get; set; }

        public string Race_Name { get; set; }

In the DB the fields Race_Date1Time and Race_Name have original names "Race date/time" and "Race Name". Thank you.

henkmollema commented 7 years ago

What's not 'sucessful'? Did the mapping not work? Did you receive an error? Please try and debug the transformation you defined.

henkmollema commented 7 years ago

I'm going to close this as it's unclear what's going wrong. Please re-open the issue if you like.