0

I have recently moved from CSVHelper version 16.0.0 to 27.2.1. It seems to me for the CSV writer the mappings are lost as they are contained in the CSVContext now, instead of the CSVConfiguration.

I would like to use a custom Mapping to write to the CSV and not an autogenerated Mapping, is there a way to do this now?

using (StreamWriter _stream = new StreamWriter(_fileLocation))
{
  using (CsvWriter _csvWriter = new CsvWriter(_stream, CsvContext.Configuration))
  {
      _csvWriter.WriteRecords(items);
      _csvWriter.Flush();
  }
}

I added the code as context, as you can see the CSVWriter only accepts the Configuration, which no longer contains the mapping, so when I check the CSVWriter's context, there is no mapping. If I check the CSVContextitself, I do see a mapping, the custom one I made earlier.

example:

public class Foo
{
    public Guid id {get;set;}   
    public string name {get;set;}
    public DateTime created {get;set;}
}

So let's say I have the class Foo as above and I create a custom class mapping using the factory so:

private static ClassMap CreateMap()
{
    Factory _factory = new Factory();
            
    IHasMap<Foo> _mapper = _factory.CreateClassMapBuilder<Foo>();       
    
    _mapper.Map(x => x.id);
    _mapper.Map(x => x.name);       
    
    return _mapper.Build();
}   

And then i register the Mapping as such:

_context.RegisterClassMap(CreateMap());

I will end up with the result:

Id,name,Date
d2e0b6d5-356c-4c34-9437-92942ee9232c,SomeName,2020-01-01

but the result I want is:

Id,name
d2e0b6d5-356c-4c34-9437-92942ee9232c,SomeName

I want to make one thing clear, this was working under version 16.0.0.0

2
  • Have you checked the documentation? The ClassMap<T> must be registered within the context, but then it should work as before. Otherwise please make a little example, containing the source class with 2 or 3 example values and the desired CSV output. Commented Dec 6, 2021 at 13:10
  • I have been using the RegisterClassMap method of the context. When I was using version 16.0.0.0 it was working as I wanted to (when the `RegisterClassMap~ was part of the configuration). I shall do my best to give an abstract example of what is happening with my code. Commented Dec 6, 2021 at 13:37

1 Answer 1

3

RegisterClassMap moved from the Configuration to the Context. You have to register it on the CsvWriter.Context.

public static void Main(string[] args)
{    
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        // Configuration settings
    };
    
    using (CsvWriter _csvWriter = new CsvWriter(Console.Out, config))
    {
        var fooRecords = new List<Foo> { new Foo { id = new Guid("d2e0b6d5-356c-4c34-9437-92942ee9232c"), name = "SomeName", created = new DateTime(2020,1,1)} };

        _csvWriter.Context.RegisterClassMap(CreateMap());

        _csvWriter.WriteRecords(fooRecords);

        _csvWriter.Flush();
        
        Console.Read();
    }            
}

public class Foo
{
    public Guid id {get;set;}   
    public string name {get;set;}
    public DateTime created { get; set; }
}

private static ClassMap CreateMap()
{
    Factory _factory = new Factory();

    IHasMap<Foo> _mapper = _factory.CreateClassMapBuilder<Foo>();

    _mapper.Map(x => x.id);
    _mapper.Map(x => x.name);

    return _mapper.Build();
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I made an obvious boneheaded oversight of not doing the mapping after the initialization of the CsvWriter. It would be nice if you can include the context right away in the constructor, I don't know if there is a reason this isn't done?
I believe the changes had to do either with performance or threading issues. I'm not sure if with the changes in Version 20.0.0, if it would work to be able to pass in the context. joshclose.github.io/CsvHelper/change-log/#section-32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.