I'm working with .NET 6 console app. I have Program.cs where the DBContext is instantiated.
public class Program
{
static ProcedureService procservice;
static void Main(string[] args)
{
var services = new ServiceCollection();
string onlineConnectionString = "Server = localhost; Database = DB; Trusted_Connection = True;";
services.AddDbContext<DBContext>(o => o.UseSqlServer(ConnectionString), ServiceLifetime.Transient);
services.AddScoped<ProcedureService , ProcedureService>();
var serviceProvider = services.BuildServiceProvider();
procservice = serviceProvider.GetService<ProcedureService>();
}
}
I also have an internal class that would call another service to do the logic and save something on the DB.
the service looks like this
public class Service
{
ProcedureService procservice;
public Service(ProcedureService procservice)
{
this.procservice = procservice;
}
}
and inside this service I have a method who also calls the ProcedureService and that's the time I had encounter "A second operation was started on this context instance before a previous operation completed."
ProcedureService
public ProcedureService(DBContext context)
{
this.context = context;
}