-
Notifications
You must be signed in to change notification settings - Fork 96
Getting an instance of ICrudServices
To use GenericServices you need to get an instance of the ICrudServices/ICrudServicesAsync. Typically this is done via dependency injection (DI), but can be done manually. There are two parts to using DI
- You register GenericServices on startup
- You inject an instance of CrudServices by using the the correct version of the ICrudServices (there are two types, each with a sync/async version).
The default usage of GenericServices is working with one DbContext.
Look at Registering GenericServices - Registering one DbContext
You can inject via a constructor, or in ASP.NET Core MVC/Web API you can also use the [FromService] to inject via an action parameter. You should use either ICrudServices
or ICrudServicesAsync
to get the sync or async version respectively.
Here is and example of injecting via constructor from the RazorPageApp in the EfCore.GenericServices repo.
EfCore.GenericServices can work with multiple DbContexts at the same time.
Look at Registering GenericServices - Registering multiple DbContext
You can inject via a constructor, or in ASP.NET Core MVC/Web API you can also use the [FromService] to inject via an action parameter. You should use either ICrudServices<TContext>
or ICrudServicesAsync<TContext>
to get the sync or async version respectively. The TContext
is the DbContext that you wish to use with this instance of the CrudServices .
Here is an example using the [FromServices] parameter injection from my EfCoreSqlAndCosmos project which uses three DbContexts.