mercredi 30 novembre 2016

In EF stream is null when seed method run with data from CSV file

I have a large dataset formated CSV file type, I want to add all the data into my EF database from CSV file. But when run seed method, it creates error Error Message is

Value cannot be null. Parameter name: stream

protected override void Seed(Drug_Information_System.Context.DrugInformationContext context)
    {

        Assembly assembly = Assembly.GetExecutingAssembly();

        string resourceName = "Drug_Information_System.Domain.SeedData.PharmaCompanies.CSV";

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                CsvReader csvReader = new CsvReader(reader);
                csvReader.Configuration.WillThrowOnMissingField = false;

                var records = csvReader.GetRecords<PharmaCompany>().ToArray();

                foreach (PharmaCompany values in records)
                {
                    context.PharmaCompanies.AddOrUpdate(values);
                }
            }
        }
        context.SaveChanges();
    }

I am not sure whats wrong in my code...

Aucun commentaire:

Enregistrer un commentaire