Search code examples
c#entity-frameworkaspnetboilerplate

Get DbContext Instance used by asp.net boilerplate repository


I started a new project using Aspnet Boiler plate, but no longer feel like using repositories and tons of hidden engineering done behind the scene.

Hence looking forward to get instance the db context instantiated by Abp repositories framework and

  1. work with Ef core LINQ queries directly.
  2. Commit my changes directly using SaveChanges, rather than relying on abstracted transactions.

How do I fetch that instance please?


Solution

    1. work with Ef core LINQ queries directly.

    You can get an instance of DbContext using GetDbContext() of IDbContextProvider.

    private readonly YourDbContext _ctx;
    
    public YourService(IDbContextProvider<YourDbContext> dbContextProvider)
    {
        _ctx = dbContextProvider.GetDbContext();
    }
    
    1. Commit my changes directly using SaveChanges, rather than relying on abstracted transactions.

    You can disable transactions by applying [UnitOfWork(IsDisabled = true)] attribute

    Here is a relevant article on transaction management with ASP.NET Boilerplate.