C# unit test using IdentityDbContext - 3 tests last very long
01:41 10 Dec 2025

I have a controller and I need to test it. The controller accepts ApplicationDbContext in its constructor, inheriting from IdentityDbContext. This IdentityDbContext accepts connection string/name in its constructor.

// AccountController
private readonly ApplicationDbContext _applicationDbContext;

public AccountController(ApplicationDbContext applicationDbContext)
{
    _applicationDbContext = applicationDbContext;
}

// ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext

public ApplicationDbContext() : base("IdentityConnection")
{
}

The IdentityConnection connection string is noted in app.config of the UnitTests project:


    

The trick is that the first three test methods (their initialize phases) are stuck on calling the constructor of IdentityDbContext for about 30 seconds, which prolongs building process by one and half minute. This is very annoying and I'd like to avoid this.

Any help is appreciated.

c# unit-testing