6

When injecting DBContext into my repository, how should the using statement look?

Ex: Startup.cs

services.AddDbContext<VisualDbContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

VisualDbContext.cs

public partial class VisualDbContext : DbContext
    {
        public VisualDbContext(DbContextOptions<VisualDbContext> options) : base(options)
        {}

        public DbSet<Template> Template { get; set; }
        public DbSet<Exercise> Exercise { get; set; }
        public DbSet<Symbol> Symbol { get; set; }
    }

Repository

public class TemplateRepository : ITemplateRepository
    {
        private readonly VisualDbContext _dbContext;
        public TemplateRepository(VisualDbContext dbContext)

        {
            _dbContext = dbContext;
        }

        public async Task<List<KeyValuePair<char, string>>> GetTemplateAsync(int templateId)
        {
                using (_dbContext) //this seems wrong...
                {
                   ...
                }
        }

2
  • Yeah I have found that and other architectural problems when using EF... For example repository leaking into other layers by means of "virtual methods and properties" that acquire "Update capabilities" same situation with Navigation properties.... For this reason we no longer use EF ever. Commented Mar 31, 2020 at 17:46
  • If you prefer to use the using statement in your repository, just don't inject your db context and instead create a new one i.e. using (VisualDbContext _dbContext = new VisualDbContext()){} Commented Mar 31, 2020 at 17:52

2 Answers 2

10

In .NET Core's DI, the DbContext will be registered as a Scoped Service, which means that its lifetime is controlled by the DI container, and you don't have to worry about it.

In ASP.NET Core the Scope is tied to the Http Request, so you'll get the same DbContext instance injected into all dependent services over the course of the request processing, and the DbContext will be Disposed at the end of the Request.

This both simplifies your code, as you can omit the initialization of the DbContext and the using blocks that are otherwise necessary, and it enables you to easily scope transactions that cross service boundaries.

Sign up to request clarification or add additional context in comments.

3 Comments

In ASP.NET Core the Scope is tied to the Http Request, so you'll get the same DbContext instance injected into all dependent services over the course of the request processing Isnt that a problem when you want to use multithreading?
If you want to use multiple threads in a single web request that all need to use the DbContext, then you wouldn't use the injected DbContext. Instead you would configure DI to give you a DbContextFactory, and use that to create local DbContext instances. learn.microsoft.com/en-us/ef/core/dbcontext-configuration/…
Thank you for the information. Would it be "good practice" to pass the context factory to another method in another class? For example if you would use the repository pattern.
0

By defining the property _dbContext you shouldn't need to use the using statement. It basically makes a new VisualDbContext while you are using the repository and it will dispose of it when you are finished using the repository.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.