Skip to content

Instantly share code, notes, and snippets.

@Jacknq
Last active January 1, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jacknq/5b12771379eba28fcacb25f6b4fb4ba3 to your computer and use it in GitHub Desktop.
Save Jacknq/5b12771379eba28fcacb25f6b4fb4ba3 to your computer and use it in GitHub Desktop.
.net startup singleton
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