Interesting comments about DI and DbContext lifetime scope

This commit is contained in:
CESARDELATORRE 2017-01-26 16:23:49 -08:00
parent fa6a273a33
commit 7ae197da32

View File

@ -45,7 +45,7 @@
services.AddMvc(options => services.AddMvc(options =>
{ {
options.Filters.Add(typeof(HttpGlobalExceptionFilter)); options.Filters.Add(typeof(HttpGlobalExceptionFilter));
}).AddControllersAsServices(); }).AddControllersAsServices(); //Controllers are also injected thru DI
services.AddEntityFrameworkSqlServer() services.AddEntityFrameworkSqlServer()
.AddDbContext<OrderingContext>(options => .AddDbContext<OrderingContext>(options =>
@ -53,7 +53,7 @@
options.UseSqlServer(Configuration["ConnectionString"], options.UseSqlServer(Configuration["ConnectionString"],
sqlop => sqlop.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name)); sqlop => sqlop.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
}, },
ServiceLifetime.Scoped ServiceLifetime.Scoped //DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request)
); );
services.AddSwaggerGen(); services.AddSwaggerGen();