daveomcd
daveomcd

Reputation: 6555

How can I create multiple new instances of a model and save them to the database?

I'm using the following code to attempt to import a CSV file. However it is just saving the last object of Fact rather than saving each of the objects that were built.

Do While Not sr.EndOfStream
  Dim aFact as Fact
  Dim mArray = sr.ReadLine().Split(",")

  aFact.Name = mArray(0)
  aFact.Value = mArray(1)
  db.Facts.Add(aFact)  
End
db.SaveChanges()

Upvotes: 0

Views: 44

Answers (1)

letsjak
letsjak

Reputation: 359

Just use a list where you save the object

Dim factList As New List(Of Fact) ' add the list
Do While Not sr.EndOfStream
  Dim aFact as Fact
  Dim mArray = sr.ReadLine().Split(",")

  aFact.Name = mArray(0)
  aFact.Value = mArray(1)
  factList.Add(aFact) ' put fact object in list  
End

Upvotes: 1

Related Questions