Skip to content

Instantly share code, notes, and snippets.

@MrAntix
Created November 28, 2017 11:48
Show Gist options
  • Save MrAntix/8668a7cf6821437589502b30681e4c70 to your computer and use it in GitHub Desktop.
Save MrAntix/8668a7cf6821437589502b30681e4c70 to your computer and use it in GitHub Desktop.
EF: Allows usual transactions for scope injected instances and a factory method to create contexts which are disposed of by the caller
services.AddDbContext<MyDataContext>(
o => o.UseSqlServer(settings.ConnectionString),
ServiceLifetime.Scoped,
ServiceLifetime.Singleton);
services.TryAddSingleton<Func<MyDataContext>>(sp =>
() => new MyDataContext(sp.GetService<DbContextOptions<MyDataContext>>())
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment