public int CreateOrder(ShoppingBag bag, string email) { var user = this.GetUser(email); var order = new Order { State = State.Received, User = user, Timestamp = DateTime.UtcNow }; var books = this.GetBooksByIds(bag.BookIds); foreach (var item in bag.BookIds) { var orderItem = new OrderItem { Order = order, Book = books.Single(x => x.BookId == item), Quantity = bag.Quantity(item) }; order.Items.Add(orderItem); } user.Orders.Add(order); this._ctx.SaveChanges(); this._diagnosticSource.Write("OnOrderCreated", new { orderId = order.OrderId }); return(order.OrderId); }