I'm using MongoDB in my project, I have created a DB context like this:
public class PortfolioDbContext
{
public readonly IMongoCollection<PortfolioData> _portfolioCollection;
public PortfolioDbContext()
{
var client = new MongoClient("mongodb://localhost:27017");
_portfolioCollection = client.GetDatabase("portfolioServiceDb").GetCollection<PortfolioData>("Portfolios");
}
}
I was to inject it into my repository class:
public PortfolioDbContext _db;
public DataService(PortfolioDbContext _db)
{
_db = this._db;
}
but db returns null, I thought I need to register it in my startup:
services.AddSingleton<PortfolioDbContext>();
but I'm still getting null, any idea why?
this._db = _dbin the constructor of DataService.