Search code examples
asp.net-coreef-core-2.2

How to create DbContext


below default code in controller working fine

public ProductController(appDbContext parmContext)
{
    _context = parmContext;
}

now I want to add DAL and in that, getting error creating object of type appDbContext, what to pass/set for parmContext?

below is the connection in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddDbContext<appDbContext>(config => 
    {
        config.UseMySql(Configuration.GetConnectionString("PreSales"));
});
}

Below is the code I want to use

public IEnumerable<ProductStatusMaster> GetProductStatusFRdal()

    // here I ant to create object of DBcontext (i.e. _context)

    {
        try
            {
                 var msm = _context.ProductStatusMaster
                    .Where(s => s.ActiveYn == 1 )
                    .OrderBy(s => s.Status)
                    .ToList();

                return msm;
            }
            catch
            {
                throw;
            }
    }

Solution

  • Let me get an answer.

    There are 2 ways to realize code you want. 1) Through Controller DI. 2) Through Service Locator pattern (antipattern).

    By the code:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddDbContext<appDbContext>(config => 
        {
            config.UseMySql(Configuration.GetConnectionString("PreSales"));
    });
    }
    

    you set up dependency for DbContext and configure connection parameters. So, for the first method you should pass the dependency through controller. Possible code will be like this:

    public class MyTestAppController
    {
         private appDbContext _context;
    
         public MyTestApController(appDbContext externalAppDbContext)
         {
             _context = externalAppDbContext;
         }
    
         public IEnumerable<ProductStatusMaster> GetProductStatusFRdal()
         {
            try
                {
                     var msm = _context.ProductStatusMaster
                        .Where(s => s.ActiveYn == 1 )
                        .OrderBy(s => s.Status)
                        .ToList();
    
                    return msm;
                }
                catch
                {
                    throw;
                }
        }
    }
    

    2) Using Service Locator pattern

    In this case you should use IServiceProvider dependency. Code example like this:

    public class MyTestAppController
    {
       private IServiceProvider _provider; 
    
       public MyTestAppController(IServiceProvider provider) => _provider = provider; 
    
       public IEnumerable<ProductStatusMaster> GetProductStatusFRdal() 
       {
          var _context = _provider.GetService<appDbContext>(); 
    
          try 
          { 
               var msm = _context.ProductStatusMaster .Where(s => s.ActiveYn == 1 ) 
                         .OrderBy(s => s.Status) .ToList(); return msm; 
          } 
          catch { throw; }
       }
    }