I create entity framework db context in startup
services.AddTransient<MyContext>(_ => new MyContext(connectionString));
private readonly MyContext context;
public ArchiveService(MyContext context)
{
this.context = context;
}
In asp.net core, all services which you registered with AddTransient
are disposed together with a scope, so - when request ends. What's the difference between Transient
and Scoped
then you might ask? For Transient
- new instance is created for every resolution. In your case - all your service classes will have distinct instances of MyContext
. All of them will be disposed when request ends. For Scoped
- only one instance will be created for given request (scope), so all your services would have shared the same instance, which is disposed when request ends.