I have an Asp.Net MVC project with Entity Framework 4 and SQL Server, where there is a Action
that receives via POST
a csv
file.
Considering memory usage, processing time and resources, or any questions about the database, which of these two code options is the most recommended?
An approximate number of 15000 rows can be used in the file.
Option 1:
var postedFile = Request.Files[0];
using (var reader = new StreamReader(postedFile.InputStream))
{
using (TransactionScope scope = new TransactionScope())
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
// Validações
db.Produtos.Add(new Produto { Descricao = values[0], Valor = values[1] });
}
db.SaveChanges();
scope.Complete();
}
}
Option 2:
var postedFile = Request.Files[0];
using (var reader = new StreamReader(postedFile.InputStream))
{
var produtos = new List<Produto>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
// Validações
var produto = new Produto
{
Descricao = values[0],
Valor = values[1]
};
produtos.Add(produto);
}
using (TransactionScope scope = new TransactionScope())
{
produtos.ForEach(p => db.Produto.Add(p));
db.SaveChanges();
scope.Complete();
}
}
Is there a Option 3 better?