我使用以下代码尝试导入CSV文件。然而,它只是保存Fact
的最后一个对象,而不是保存每个构建的对象。
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()
答案 0 :(得分:1)
只需使用保存对象的列表
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