title: How to create class model for multiple collection in C#?
body: <p>I trying to create web services for shop cart with <code>mongodb</code> and <code>dotnet core</code> web api. My cart collection have information of product. Product must be a multiple array or object and I tried with one solution but I have this error : <br>
  <strong> An unhandled exception occurred while processing the request.
  InvalidOperationException: Unable to resolve service for type 'CartService' while attempting to activate 'CartsController'. </strong></p>
  
  <p>This is looks collection of cart:</p>
  
  <pre><code>{
      "_id" : ObjectId("5cab18a057ab66f2536feeb9"),
      "Status" : "Inactive",
      "Product" : [ 
          {
              "_id" : ObjectId("5ca9b27dbec46268305ce427"),
              "Quantity" : 1.0,
              "Name" : "Samsung",
              "Price" : 1000.0
          }, 
          {
              "_id" : ObjectId("5ca9b27dbec46268305ce427"),
              "Quantity" : 2.0,
              "Name" : "Samsung",
              "Price" : 9999.0
          }
      ],
      "modified_on" : "5/4/2019"
  }
  </code></pre>
  
  <p>This is class model:</p>
  
  <pre><code>public class Cart
  {   
      [BsonId]
      [BsonRepresentation(BsonType.ObjectId)]
      public string Id { get; set; }
  
      [BsonElement("Status")]
      public string Status { get; set; }
  
      [BsonElement("Product")]
      public Product Product { get; set; }
  
      [BsonElement("modified_on")]
      public DateTime modified_on { get; set; }
  }
  </code></pre>
  
  <p>Cart Service:</p>
  
  <pre><code>public class CartService
  {
      private readonly IMongoCollection&lt;Cart&gt; _carts;
  
      public CartService(IConfiguration config)
      {
          var client = new MongoClient(config.GetConnectionString("WebShopDb"));
          var database = client.GetDatabase("WebShopDb");
          _carts = database.GetCollection&lt;Cart&gt;("Carts");
      }
  
      public List&lt;Cart&gt; Get()
      {
          return _carts.Find(cart =&gt; true).ToList();
      }
  
      public Cart Get(string id)
      {
          return _carts.Find&lt;Cart&gt;(cart =&gt; cart.Id == id).FirstOrDefault();
      }
  
      public Cart Create(Cart cart)
      {
          _carts.InsertOne(cart);
          return cart;
      }
  
      public void Update(string id, Cart cartIn)
      {
          _carts.ReplaceOne(cart =&gt; cart.Id == id, cartIn);
      }
  
      public void Remove(Cart cartIn)
      {
          _carts.DeleteOne(cart =&gt; cart.Id == cartIn.Id);
      }
  
      public void Remove(string id)
      {
          _carts.DeleteOne(cart =&gt; cart.Id == id);
      }
  }
  </code></pre>
  
  <p>Cart Controller:</p>
  
  <pre><code>[Route("api/[controller]")]
  [ApiController]
  public class CartsController : ControllerBase
  {
      private readonly CartService _cartService;
  
      public CartsController(CartService cartService)
      {
          _cartService = cartService;
      }
  
      [HttpGet]
      public ActionResult&lt;List&lt;Cart&gt;&gt; Get()
      {
          return _cartService.Get();
      }
  
      [HttpGet("{id:length(24)}", Name = "GetCart")]
      public ActionResult&lt;Cart&gt; Get(string id)
      {
          var cart = _cartService.Get(id);
  
          if (cart == null)
          {
              return NotFound();
          }
  
          return cart;
      }
  
      [HttpPost]
      public ActionResult&lt;Cart&gt; Create(Cart cart)
      {
          _cartService.Create(cart);
  
          return CreatedAtRoute("GetCart", new { id = cart.Id.ToString() }, cart);
      }
  
      [HttpPut("{id:length(24)}")]
      public IActionResult Update(string id, Cart cartIn)
      {
          var cart = _cartService.Get(id);
  
          if (cart == null)
          {
              return NotFound();
          }
  
          _cartService.Update(id, cartIn);
  
          return NoContent();
      }
  
      [HttpDelete("{id:length(24)}")]
      public IActionResult Delete(string id)
      {
          var cart = _cartService.Get(id);
  
          if (cart == null)
          {
              return NotFound();
          }
  
          _cartService.Remove(cart.Id);
  
          return NoContent();
      }
  }
  </code></pre>