Last active
January 1, 2021 14:15
-
-
Save Jacknq/5b12771379eba28fcacb25f6b4fb4ba3 to your computer and use it in GitHub Desktop.
.net startup singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddDbContextPool<dbContext>(options => options.UseSqlite(conn)); | |
//.UseSqlServer(connection)); | |
//with scoped | |
//services.AddScoped<DmServices>(); | |
//with singleton | |
// services.AddSingleton<DmServices>(s => | |
// new DmServices(new ProsperusContext( | |
// new DbContextOptionsBuilder<dbContext>().UseSqlite(conn).Options))); | |
services.AddSingleton<DmServices>(s => | |
{ | |
using (var scope = s.CreateScope()) | |
{ | |
var dbContext = scope.ServiceProvider.GetService<dbContext>(); | |
var lastItem = dbContext;//.Items.LastOrDefault(); | |
return new DmServices(lastItem); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment