Base solution for your next web application
Open Closed

Get access to the context when using IRepository<T> #6056


User avatar
0
antonis created

Hi,

Based on the documentation on this link when you use a repository: "You can also use the Context object in the repository methods to reach your DbContext and directly use Entity Framework APIs"

However I dont see how is this possible as no Context property exists on my IRepository<T, int>

Any tips?

Thanks in advance


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @antonis

    It is valid for custom repositories. So, if you crete a custom repository inheriting from *RepositoryBase class, then you can access dbContext inside from repository method. Maybe it is not expressed clearly here.

    If you are using a generic repository and want to access DbContext in an app service or controller, you can inject IDbContextProvider<TDbContext> and use it to access the DbContext.

  • User Avatar
    1
    aaron created
    Support Team
    // using Abp.EntityFrameworkCore.Repositories;
    
    var context = _repository.GetDbContext();
    
  • User Avatar
    0
    nicolaslau created

    @ismcagdas once i want to inject IDbContextProvider to AppService`s ctor, get an exception 'could not proxy xxxxAppService'. where and how to get DbContext?