2

It's any possible to using new dependency injection mechanism in ASP.NET Core to inject IdentityDbContext into my repository?

EF version:

  "EntityFramework.Commands": "7.0.0-rc1-final",
  "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final", 

Context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>,       IApplicationDbContext
{
    public DbSet<Customer> Customers{ get; set; }

    protected override void OnModelCreating(ModelBuilder builder) => base.OnModelCreating(builder);
}

Repository:

 public class CustomerRepository: ICustomerRepository
{
    private readonly IApplicationDbContext _context;

    public CustomerRepository(IApplicationDbContext context)
    {
        _context = context;
    }

    public List<Customer> GetAll()
    {
        return _context.Customers.ToList();
    }
}

How to configure Startup.cs?

I have basic configuration

        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

this does not work:

        services.AddTransient<IApplicationDbContext, ApplicationDbContext>();

2 Answers 2

3

You really don't need to define IApplicationDbContext in order for the dependency injection framework to work.

You can try

Startup.cs:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

    // You can actually use
    // Configuration.GetConnectionString("DefaultConnection") here instead to simplify the code

If you look at the second parameter of AddDbContext(), ServiceLifetime is set to Scoped, which means the object instance will be the same within the same request, but different among different requests.

If you want to the DI framework to create different instances every single time, you can set the second parameter to Transient.

Repository:

public class CustomerRepository: ICustomerRepository
{
    private readonly ApplicationDbContext _context;

    public CustomerRepository(ApplicationDbContext context)
    {
        _context = context;
    }

    public List<Customer> GetAll()
    {
        return _context.Customers.ToList();
    }
}

The DI framework will inject ApplicationDbContext for you automatically.

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

Comments

0

Using the services.AddTransient<IApplicationDbContext, ApplicationDbContext>(); syntax asks the dependency injection block to directly instantiate ApplicationDbContext, which bypasses the DB context factory set up by EntityFramework.

Instead, you can use this syntax, that will preserve how ApplicationDbContext is internally registered by AddDbContext:

services.AddTransient<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());

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.