Search code examples
c#automapperbulkinsert

BulkInsertAsync using AutoMapper C#


I have rows of data that are coming from a csv file that I want to bulk insert into my database. I am using automapper to map the data back to its desired Properties and Data Types in the Entity Class. I have confirmed using the debugger that my entityRecord is returning the desired values, but am having trouble with the BulkInsertAsync. It is returning this error " The type arguments for method 'method' cannot be inferred from the usage. Try specifying the type arguments explicitly." Here is what I have tried below

        public async Task PostMyTaskAsync(string myResponses)
    {
        var mapper = MapperConfig.InitializeAutomapper();
        IEnumerable dtoRecords = null;

        var config = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            PrepareHeaderForMatch = args => args.Header.ToLower(),
        };

        using (StringReader sr = new StringReader(myResponses))
        using(var csv = new CsvReader(sr, config))
        {
            dtoRecords = csv.GetRecords<MyDtoClass>();
            int line = 0;

       foreach (var dtoRecord in dtoRecords)
            {
                MyEntityClass entityRecord = new();
                if (++line >=3)
                {
                    entityRecord = mapper.Map<MyEntityClass>(dtoRecord);
                    await _cloudDWHDbContext.BulkInsertAsync(entityRecord);
                }
            }
        }

Solution

  • I think you need to specify the entity you are trying to insert.

    _cloudDWHDbContext.BulkInsertAsync<Entity>(entityRecord)