Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CsvHelper error when using RegisterClassMap

I've based the mappings for my CSV file based off the official docs here: https://joshclose.github.io/CsvHelper/getting-started

here are my class I'm using

public class DropShippperCSV
{
    public string PurchaseOrderNumber { get; set; }
    public int ReleaseNumber { get; set; }
    public int LineNumber { get; set; }
    public string DeliveryCompanyName { get; set; }
    public string DeliveryCompanyTrackingNumber { get; set; }
}

public class DropShippperCSVMap : ClassMap<DropShippperCSV>
{
    public DropShippperCSVMap()
    {
        Map(m => m.PurchaseOrderNumber).Column("Delivery Company Tracking Number");
        Map(m => m.ReleaseNumber).Column("Release Number");
        Map(m => m.LineNumber).Column("Line Number");
        Map(m => m.DeliveryCompanyName).Column("Delivery Company Name");
        Map(m => m.DeliveryCompanyTrackingNumber).Column("Delivery Company Tracking Number");
    }
}

then calling on it like so

                var reader = new StreamReader(file.OpenReadStream());
                var csv = new CsvReader(reader);

                csv.Configuration.RegisterClassMap<DropShippperCSVMap>();
               var records = csv.GetRecords<DropShippperCSV>().ToList();

and I'm getting this error

Error CS0311 The type 'DropShippperCSVMap' cannot be used as type parameter 'TMap' in the generic type or method 'IReaderConfiguration.RegisterClassMap()'. There is no implicit reference conversion from 'DropShippperCSVMap' to 'CsvHelper.Configuration.ClassMap'.

I've based directly off the offical docs and I can't tell what I did wrong

like image 438
Bomani Kernizan Avatar asked Dec 09 '25 00:12

Bomani Kernizan


2 Answers

Use csv.Context.RegisterClassMap<DropShippperCSVMap>(); instead of csv.Configuration.RegisterClassMap<DropShippperCSVMap>();.

like image 89
Kanika Sharma Avatar answered Dec 10 '25 12:12

Kanika Sharma


I droped FluentNHibernate and RegisterClassMap entirely and used CsvHelper.Configuration.Attributes Name attribute to do the mapping instead

like image 38
Bomani Kernizan Avatar answered Dec 10 '25 12:12

Bomani Kernizan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!