first of all my program: I have a console application which starts a thread and a webapplication.
The thread is receiving events and has to validate and insert some things in the database. The webapplication receives api calls and has to have access to the database aswell, validating and inserting. Both are communicating between each other aswell.
My problem now is that I'm not really understanding a good way to use the DbContext in both "applications".
I've tried to receive the DbContext inside the thread from the webapplication via:
app.Services.GetService<DbContext>();
This doesn't work and just freezes the thread.
Is it an acceptable way to DI in webapplication in controllers and use this is the thread?
using(var context = new DbContext())
I'm just thinking if there is a better way to not always create a new connection and use the same dbcontext as in the webapplication.
This is my DbContext
public class DataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionstring);
}
public DbSet<Class> Name { get; set; }
public DbSet<Class> Name { get; set; }
}
I hope you guys could understand me, my english isn't the best.
Thanks in advance for taking the time to read and help!