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
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.RegisterClassMapmethod 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.