stanislaw-polanski
stanislaw-polanski

Reputation: 23

What is "DbContextOptions`1"?

I have Web API in ASP .NET Core. When I add a db context in Startup.ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<FixturesContext>(
            options => options.UseSqlServer(Configuration.GetConnectionString("FixturesDatabase")));
    services.AddControllers();
}

I see the number of services in the "services" container raises by three, I think those are:

I am curious what is "DbContextOptions1"? Does anyone know? I have tried googling it but not satysfying result. My goal is to replace original context with in-memory (to run integration tests without original database), so I'm deleting db context and its options and adding in-memory context instead of them.

Upvotes: 2

Views: 961

Answers (2)

Nkosi
Nkosi

Reputation: 247008

DbContextOptions'1 would be the generic DbContextOptions<FixturesContext> registered to be injected into the context when being initialized.

Reference Configuring DbContextOptions

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

    //...
}

Upvotes: 3

Morris Janatzek
Morris Janatzek

Reputation: 672

The third service you are getting is a generic version of the DbContextOptions. When calling .ToString() on a generic type it often looks like this.

The reason why there are three instances is that EF adds a general DbContextOptions object and a more specific one for your defined context.

If you inspect the calls of the third service you should find the type of your DbContext as a generic parameter.

Upvotes: 3

Related Questions