I'm doing a program Console
, where basically it executes these 4 procedures
IEnumerable<_Url>
But I'm getting an error:
> New transaction is not allowed because there are other threads running in the session.
My program console looks like this:
private static void Main(string[] args)
{
IEnumerable<Urls> registros = db.UrlsTable.Where(w => w.Lido == false).Take(10);
ExecutaTarefasAsync(registros).Wait();
}
public static async Task ExecutaTarefasAsync(IEnumerable<Urls> registros)
{
var urlTasks = registros.Select((registro, index) =>
{
//parsing html
var modelProduto = ExtraiDados.ParserHtml(registro.Url);
db.Produto.Add(modelProduto);
//save Database
var downloadTask = db.SaveChangesAsync();
//marca url como lida
registro.Lido = true;
db.Entry(registro).State = EntityState.Modified;
db.SaveChangesAsync();
return downloadTask;
});
await Task.WhenAll(urlTasks);
}
Initially I'm doing with EntityFramework because it would be the easiest and then would refactor to Dapper or Ado.Net for maximum performance.
I do not know if I can ask a 2nd question, but if I do with Ado.Net I would have to create a Parallel.ForEach
function to do the insert?
Today the problem is that I download the photos and this step takes longer than the parsing, so I want to use async correctly to maximize the process, because there are thousands of records every day.