流利的映射 - CsvHelper

Fluent Mapping - CsvHelper

我正在使用由 Josh Close 构建的 CsvHelper 库。我认为我将在其中获取 .csv 文件的数据源有一种方法可以使用适当的命名约定来屏蔽或 'alias' 列 headers。不幸的是,情况并非如此,所以我想使用 Fluent Class 映射,但我不清楚如何实现它。

我构建了下面的 class(对此进行了简化 post)

 public class PaymentType
{
    public int PaymentTypeId { get; set; }
    public string BusinessUnit { get; set; }
    public string Region { get; set; }
    public string Status { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public decimal Amount { get; set; }
    public string Name { get; set; }
}

另一个class中的以下方法下载并保存文件

private string DownloadDS(string getURL, string fileName)
    {
        try
        {
            //CSV path
            string url = getURL;

            //Where to save and retrieve CSV file
            string path = AppDomain.CurrentDomain.BaseDirectory + "ImportedFiles\" + fileName;

            //Download file and save it 
            WebClient client = new WebClient();
            client.DownloadFile(url, path);



            return path;
        }
        catch
        {



            return "";
        }

    }

此方法然后更新数据库

 private void UpdateDB(string path, string fileName)
    {
        try
        {
            //read in file with CsvReader object and output to an enumerable object
            var csv = new CsvReader(new StreamReader(path));
            var importedPaymentTypes = csv.GetRecords<ImportPaymentTypes>();

            //Save each payment type record to the db.  If record exhsists, update it, if not, add it.


           ...

        }
        catch
        {
            ...


        }

    }

我删除了一些日志记录和数据库逻辑以缩短代码片段。我阅读了 Fluent Mapping,但对如何实施它感到困惑?我明白我要构建一个class,但是你reference/configure如何使用映射器class?

这是来自 Josh 网站的示例;

http://joshclose.github.io/CsvHelper/#reading-reading-all-records

看看映射部分。 http://joshclose.github.io/CsvHelper/#mapping

您通过以下方式注册映射:

csv.Configuration.RegisterClassMap<PaymentTypeMap>();