BookStoreContext.cs 769 B

12345678910111213141516171819202122232425
  1. using MongoDB.Driver;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MongoDB_NET5
  9. {
  10. public class BookStoreContext
  11. {
  12. private IMongoDatabase _db { get; set; }
  13. private MongoClient _mongoClient { get; set; }
  14. public BookStoreContext()
  15. {
  16. _mongoClient = new MongoClient(ConfigurationManager.ConnectionStrings["BookStore"].ConnectionString);
  17. _db = _mongoClient.GetDatabase(ConfigurationManager.ConnectionStrings["BookStore"].Name);
  18. }
  19. public IMongoCollection<T> GetCollection<T>(string nameCollection)
  20. {
  21. return _db.GetCollection<T>(nameCollection);
  22. }
  23. }
  24. }