0

i want to seed the db from a text file where on each row i have a word(aprox 70000) ex: word1(next line) word2(next line) word3(next line)

i'm guessing i have to use the seed method like i add something else for example

protected override void Seed(BabyStore.DAL.StoreContext context)
 {
 var categories = new List<Category>
 {
 new Category { Name = "Clothes" },
 new Category { Name = "Play and Toys" },
 new Category { Name = "Feeding" },
 new Category { Name = "Medicine" },
 new Category { Name= "Travel" },
 new Category { Name= "Sleeping" }
 };
 categories.ForEach(c => context.Categories.AddOrUpdate(p => p.Name, c));
 context.SaveChanges();

the table would be something like words. so i would have i guess a table with 70000 words. so i need to read the file and upload those words to be a name for each entry in db?(this is my own thinking but i do't kniow how to do it)

my question is how can i use a text file to seed here? or do i have to do it in another way?(code first) Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sss
  • 49
  • 7
  • depending on your db, you can simply import the file directly. no coding needed. – Shai Cohen Mar 09 '20 at 21:06
  • 1
    I don't agree this question is a duplicate. OP is not asking how to read a file, he is asking how to bulk insert. – Shai Cohen Mar 09 '20 at 21:09
  • @shai it's a DB inside a visual studio folder, i created all using code first,locally in the folder and i'm trying to do it this way. i remember trying some time ago with sql directly, which is not very complicated but then i had a separate DB in sql server – Sss Mar 09 '20 at 21:09
  • well good luck getting a response now – Sss Mar 09 '20 at 21:12
  • i don't know in that regard, i'm sorry. yeah, not sure why this question was closed. – Shai Cohen Mar 09 '20 at 21:14
  • 1
    You use [this answer](https://stackoverflow.com/a/54456887/578411) and then use `foreach(var line in lines) categories.Add(new Category { Name = line });`. – rene Mar 09 '20 at 21:42
  • that was an example, i have another contex here. I tried this but doesn't seem to work: List wordslist = System.IO.File.ReadLines(@"C:\Users\etc\brit.txt").ToList(); wordslist.ForEach(c => context.Anagrams.AddOrUpdate(p => p.Name)); context.SaveChanges(); – Sss Mar 10 '20 at 21:43

0 Answers0