Base solution for your next web application
Open Closed

How do I Inject/ access my EFDbContext from controller #8736


User avatar
0
mhdevelop created

Hello, I have a situation where i need to inject or access my EF DbContext from controller, please let me know how to access it . I have tried the below var ctx = IocManager.Instance.Resolve<PLCMDbContext>(); it gives error that it had depedency on options

Also I tried controller construction inject and get same error. public constructor( IDbContextProvider<MyDbContext> dbContextProvider) { _ctx = dbContextProvider.GetDbContext(); }

get same error message.

Thanks


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

    You should use repositories instead of DbContext. Of course repositories can also get DbContext.

    using Abp.EntityFrameworkCore.Repositories;
    
     _xxxRepository.GetDbContext()
    
  • User Avatar
    0
    mhdevelop created

    Where do i use this code? in controller? How do inject _xxxRepository into controller? Any example would help. we are using asp.net core and jquery

    Thanks v

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can check injection sample here https://aspnetboilerplate.com/Pages/Documents/Repositories.

    You can inject it to your Controller.