Configure Ef Core db context to be injected as separate instance for each class rather than per call, is it feasible? I could not find any overload to achieve that.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")).EnableSensitiveDataLogging());
}
The below code will make sure to inject a new instance everytime we call IDBContext object. We need to create a class (repository class) which accepts IDBContext as a constructor parameter such that runtime will inject a new instance every time a call to this class.
services.AddDbContext<MyDbContext>(
options => options.UseSqlServer("<connection string>"),
ServiceLifetime.Transient);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.