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?
AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET.
The AddDbContext extension method registers DbContext types with a scoped lifetime by default.
IDesignTimeDbContextFactory<TContext> Interface Implement this interface to enable design-time services for context types that do not have a public default constructor. At design-time, derived DbContext instances can be created in order to enable specific design-time experiences such as Migrations.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With