public bool LoadUserDataFromCsv()
        {
            var result = false;

            try
            {
                if (!_context.Users.Any())
                {
                    string rootDirectoryPath   = AppDomain.CurrentDomain.BaseDirectory;
                    string pathFinalAccessFile = Path.Combine(rootDirectoryPath, "App_Data", ConfigurationReader.ReadAppConfig("CsvData.csv", "CsvData.csv"));

                    using (var textReader = new StreamReader(pathFinalAccessFile))
                    {
                        var users = CsvHelpers.ReadFileWithoutHeader <UserDto>(textReader, typeof(UserCsvMap));

                        if (users != null && users.Any())
                        {
                            _context.Users.AddRange(_mapper.Map <IEnumerable <User> >(users));

                            _context.SaveChanges();

                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Log error
            }

            return(result);
        }
Exemplo n.º 2
0
        public void ReadFileTest()
        {
            var baseDir  = AppDomain.CurrentDomain.BaseDirectory;
            var filePath = Path.Combine(baseDir, "TestFiles", "CsvData.csv");

            var csvTestDataTextReader = new StreamReader(filePath, System.Text.Encoding.UTF8);

            var users = CsvHelpers.ReadFileWithoutHeader <UserDto>(csvTestDataTextReader, typeof(UserCsvMap));

            Assert.IsTrue(users?.Count() > 0);
        }