Home > Enterprise >  CsvHelper: Writing CSV when using generics fails
CsvHelper: Writing CSV when using generics fails

Time:11-02

C# 10 | .NET 6 | CsvHelper 29.0.0

I am attempting to use CsvHelper to write CSVs of generic types:

    public async Task RunAsync<T>()
    {
        List<T> dataList = default!;

        dataList = await ApiService.GetDataAsync<T>(Job.Read);
        
        try
        {
            string path = $"./inbox/read.{typeof(T).Name}.csv";

            CsvConfiguration config = new(System.Globalization.CultureInfo.InvariantCulture);

            using StreamWriter sw = new(path, append: false);

            using CsvWriter csvWriter = new(sw, config);

            _ = csvWriter.Context.AutoMap<T>();

            await csvWriter.WriteRecordsAsync(dataList);

        }
        catch
        {
            ...
        }
    }

For all types I try, this fails with the following error:

No properties are mapped for type 'Ecms.Common.Models.HRTEMP'. IWriter state:
Row: 2
Index: 0
HeaderRecord: 2

One of the types I am trying to write is HRTEMP:

public class HRTEMP
{
    public string? ABBRV; // Abbrv
    public DateTime? ADDDATE; // Added Date
    public string? ADDR1; // Addr 1
    public string? ADDR2; // Addr 2
    public string? ADDR3; // Addr 3
    public DateTime? ADJHIREDATE; // Adj Hire Date
    public int? AREACODE; // A/C
    public string? BUSUFFIX; // Bus Suffix
    public int? CELLPHAC; // Cell A/C
    public int? CELLPHNO; // Cell No
    public string? CITY; // City
    public string? CNTRYCODE; // Country Code
    public string? COBRASNTFLG; // COBRA Ltr Sent
    public int? COMPANYNO; // Src Com
    public int? CONTACTAC; // Contact A/C
    public string? CONTACTNAME; // Contact Name
    public int? CONTACTPHONE; // Contact Phone
    public int? COUNTYCODE; // County Code
    public int? DEPTNO; // Dept No
    public DateTime? DISABILITYDT; // Disability Date
    public int? DIVISIONNO; // Src Div
    public string? ELGBRHIRE; // Elig To Rehire
    public DateTime? ELIGSCKACCRL; // Sick Elig Date
    public int? EMPCNCID; // Emp SYTCNC Id
    public int? EMPCOMPANY; // Emp Com No
    public int? EMPDIVISION; // Emp Div No
    public int? EMPLCLASS; // Empl Cls
    public string? EMPLNAME; // Empl Name
    public int? EMPLOYEENO; // Empl No
    public string? EMPLTYPE; // Empl Type
    public DateTime? ESTAVAILDATE; // Est Avail Date
    [JsonPropertyName("EXPIREDATE")]
    public DateTime? EXPRIEDATE; // Expiration Date
    public string? FIRSTNAME25; // First Name
    public DateTime? HOLELIGDATE; // Hol Elig Date
    public int? HRTEMPID; // HRTEMP Rec Id
    public int? HRTOCCID; // 
    public DateTime? LASTDAYWK; // Last Date Wkd
    public string? LASTNAME25; // Last Name
    public DateTime? LASTYEDATE; // Last Y/E Date
    public int? LVLCODE; // LBCC Level Security
    public string? MIDDLENAME1; // Middle Name 1
    public string? MIDDLENAME2; // Middle Name 2
    public string? OCCUPDESC1; // Occupation Desc 1
    public string? OCCUPDESC2; // Occupation Desc 2
    public string? OFFICERSCODE; // Com Officer
    public DateTime? ORIGHIREDATE; // Orig Hire Date
    public int? PHONENO; // Phone No
    public int? PRTECLID; // PRTECL Rec ID
    public int? PRTLBRID; // PRTLBR Rec ID
    public int? PRTMSTID; // PRTMST Rec Id
    public int? PRTTRMID; // PRTTRM Rec ID
    public DateTime? REHIREDATE; // Re-Hire Date
    public DateTime? RETIREDDATE; // Retired Date
    public DateTime? REVIEWDATE; // Review Date
    public DateTime? SICKACRLDATE; // Sick Accrl Date
    public int? SRCCNCID; // Src SYTCNC Id
    public string? STATECODE; // St/Prov
    public string? STATUSCODE; // Record Code
    public int? SUPERVISOR1; // Supv 1 Empl
    public int? SUPERVISOR1CO; // Supv 1 Com
    public int? SUPERVISOR1DV; // Supv 1 Div
    public int? SUPERVISOR2; // Supv 2 Empl
    public int? SUPERVISOR2CO; // Supv 2 Com
    public int? SUPERVISOR2DV; // Supv 2 Div
    public int? SUP1MSTID; // Supv 1 PRTMSTID
    public int? SUP2MSTID; // Supv 2 PRTMSTID
    public int? SYTTXTID; // SYTTXT Rec Id
    public int? TERMCODE; // Term Code
    public DateTime? TERMDATE; // Term Date
    public int? TERMRSN; // Term Reason Code
    public string? UPDATEDBY; // UpDated By User
    public DateTime? UPDDATE; // Last UpDated
    public string? UPDPGM; // UpDated By Pgm
    public DateTime? VACELIGDATE; // Vac Elig Date
    public string? ZIPCODE; // Zip/Postal Code

    public HRTEMP() {}
}

If I change csvWriter.Context.AutoMap<T>() to csvWriter.Context.AutoMap<HRTEMP>(), I get the same error.

What am I doing wrong?

CodePudding user response:

Serializers in C# often rely on properties instead of fields. The error message suggests this about your class, which has no properties:

No properties are mapped for type 'Ecms.Common.Models.HRTEMP'.

Use properties instead of fields in your class:

public class HRTEMP
{
    public string? ABBRV { get; set; }
    public DateTime? ADDDATE { get; set; }
    // etc.
}
  • Related