Sorry for this question as I'm new to asp.net. I need to make Import Csv files into database that is made in sql server and added to web application by entity framework. I managed to export it, but have no clue how to import it other than maybe using StreamReader with regex. Tried using Lumenworks csvreader, but it doesn't cooparate with entity.
1 Answer
You can look at the CsvHelper NuGet package. You can find parts of the code you need to use here.
Relevant code:
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.WillThrowOnMissingField = false;
var countries = csvReader.GetRecords<Country>().ToArray();
context.Countries.AddOrUpdate(c => c.Code, countries);
}