locked
DbContext is null in Dependency injection RRS feed

  • Question

  • User448619955 posted

    I'm trying to use DI in my application but I get null value in my dbcontext.

        [Export]
        [PartCreationPolicy(CreationPolicy.NonShared)]
        public class MyController : ApiController
        {
            private static ILog log = LogManager.GetLogger(nameof(MyController));
            
            public IProductService ProductService { get; set; }
    
            [ImportingConstructor]
            public MyController()
            {
                ProductService = new ProductService();
            }
    
    
    ...
    ...
    ...
     public class ProductService : IProductService
        {
            public ProductdbContext ProductdbContext { get; set; }// is null
    ...
    ...
    ...
        [Export(typeof(ProductdbContext))]
        [PartCreationPolicy(CreationPolicy.NonShared)]
        public class ProductdbContext : DbContext
        {
            public ProductdbContext ()
            {
            }
    
            public ProductdbContext (ProductdbContextOptions<ProductdbContext> options) : base(options)
            {
                
            }
    
            [ImportingConstructor]
            public ProductdbContext (ProductdbContextDbOptions options)
                : base(options.Options)
            {
            }

    Wednesday, August 12, 2020 12:00 PM

All replies

  • User-474980206 posted

    As your controller is creating the dbcontext and not using di, it’s it’s job to pass options to the constructor. Typically you would add the options to the services, and the dbcontext to the service and let the service construct and inject.

    Wednesday, August 12, 2020 2:36 PM
  • User-17257777 posted

    Hi vahid.ch,

    You just define a ProductdbContext type property in the ProductService class. For how to DI the dbcontext, you can do it like below:

    public class ProductService : IProductService
    {
        private readonly ProdctDbContext _db;
    
        public ProductService(ProdctDbContext db)
        {
            _db = db;
        }
    
        //...
    
    }

    Best Regards,

    Jiadong Meng

    Thursday, August 13, 2020 2:28 AM
  • User-2054057000 posted

    You have to register the DbContext file in the Startup.cs class also. See DbContext Class in Entity Framework Core

    Thursday, August 13, 2020 4:39 AM
  • User448619955 posted

    I should mention I use MEF to inject the dependencies.

    I changed my code now I'm getting another error:

    Type '.........ArticleController' does not have a default constructor.
    using System.ComponentModel.Composition;
    ..
    ..
         [Export]
        [PartCreationPolicy(CreationPolicy.NonShared)]
        public class ArticleController : ApiController
        {
    
            
            public IProductService ProductService { get; set; }
    [ImportingConstructor] public ArticleController(IProductService productService) { ProductService = productService; } // my ProductService class and interface public interface IProductService { Task<Product> AddArticle(CreateProductViewModel createViewModel, string userId); ... ... public class ProductService : IProductService { public ProductDbContext ProductPdbContext { get; set; } [ImportingConstructor] public ProductService(ProductDbContext productPdbContext ) { ProductPdbContext = productPdbContext ; }

    Thursday, August 13, 2020 6:45 AM