LINQ2Vodka
LINQ2Vodka

Reputation: 3036

Where is .AddDbContext method?

Everywhere it's said to call services.AddDbContext<> method but it is not recognized inside the ConfigureServices(IServiceCollection services) method.
What am I doing wrong?

Upvotes: 10

Views: 16064

Answers (2)

Milo
Milo

Reputation: 3443

Note when actually writing the using you only have to reference Microsoft.EntityFrameworkCore omitting the specific package name. Providing it seems to throw an error.

Upvotes: 0

Tseng
Tseng

Reputation: 64131

You have to reference the correct package first, which depends on the EF Core provider you want to use.

Microsoft.EntityFrameworkCore.SqlServer for SQL Server, Microsoft.EntityFrameworkCore.Sqlite for SQLite and Microsoft.EntityFrameworkCore.InMemory for in memory (only for testing).

These are the official out-of-the-box providers. There are also 3rd party providers for PostgreSQL, MySQL, etc. The documentation providers a list of available 3rd party providers here.

Also depending on the provider you may also need to declare a certain namespace. The built-in providers are declared in Microsoft.Extension.DependencyInjection namespace so you need to add a using Microsoft.Extension.DependencyInjection; to the top of your Startup.cs.

Other providers (Oracle's MySQL provider for example) uses MySQL.Data.EntityFrameworkCore.Extensions namespace, so you need to define this using using MySQL.Data.EntityFrameworkCore.Extensions;

Upvotes: 18

Related Questions