public MagnovaultDbRepository()
 {
     context = new DataDbContext();
 }    //ctor----------------------------------------------------------------------------------------------
        public async Task <ViewOrder[]> GetOrdersAsync()
        {
            using (DataDbContext context = new DataDbContext())
            {
                Order[] orderData = context.Orders
                                    .ToArray();

                if (orderData == null)
                {
                    return(null);
                }
                else
                {
                    ViewOrder[] vieworders = new ViewOrder[orderData.Length];
                    using (ApplicationDbContext appcontext = new ApplicationDbContext())
                    {
                        for (int i = 0; i < orderData.Length; i++)
                        {
                            string          userId  = orderData[i].CustomerId;
                            ApplicationUser appuser = appcontext.Users.FirstOrDefault(x => x.Id == userId);
                            vieworders[i] = new ViewOrder
                            {
                                Id                = orderData[i].Id,
                                OnDate            = orderData[i].OnDate,
                                CustomerId        = orderData[i].CustomerId,
                                FirstName         = (appuser == null) ? "" : appuser.FirstName,
                                LastName          = (appuser == null) ? "" : appuser.LastName,
                                Street            = (appuser == null) ? "" : appuser.Street,
                                City              = (appuser == null) ? "" : appuser.City,
                                State             = (appuser == null) ? "" : appuser.State,
                                Zip               = (appuser == null) ? "" : appuser.Zip,
                                Email             = (appuser == null) ? "" : appuser.Email,
                                Phone             = (appuser == null) ? "" : appuser.PhoneNumber,
                                BestMethod        = (appuser == null) ? "" : appuser.BestMethod,
                                ShipFirstName     = orderData[i].ShipFirstName,
                                ShipLastName      = orderData[i].ShipLastName,
                                ShipStreet        = orderData[i].ShipStreet,
                                ShipCity          = orderData[i].ShipCity,
                                ShipState         = orderData[i].ShipState,
                                ShipZip           = orderData[i].ShipZip,
                                Campaign          = orderData[i].Campaign,
                                Subtotal          = orderData[i].Subtotal,
                                ShipHand          = orderData[i].ShipHand,
                                Tax               = orderData[i].Tax,
                                Total             = orderData[i].Total,
                                TransactionId     = orderData[i].TransactionId,
                                ResponseCode      = orderData[i].ResponseCode,
                                AuthorizationCode = orderData[i].AuthorizationCode,
                                CardLastFour      = orderData[i].CardLastFour,
                                Deferred          = orderData[i].Deferred,
                                Refunded          = orderData[i].Refunded,
                                Done              = orderData[i].Done,
                                Items             = new List <ViewOrderItem>()
                            };

                            //add order items
                            int oid           = orderData[i].Id;
                            var orderItemData = context.OrderItems
                                                .Join(context.Products, o => o.ProductId, p => p.Id, (orderItem, product) => new { OrderItem = orderItem, Product = product })
                                                .Where(x => x.OrderItem.OrderId == oid)
                                                .ToArray();

                            if (orderItemData != null)
                            {
                                for (int j = 0; j < orderItemData.Length; j++)
                                {
                                    ViewOrderItem vieworderitem = new ViewOrderItem
                                    {
                                        Id                = orderItemData[j].OrderItem.Id,
                                        OrderId           = oid,
                                        ProductId         = orderItemData[j].OrderItem.ProductId,
                                        ProductName       = orderItemData[j].Product.Name,
                                        ProductShortDescr = orderItemData[j].Product.ShortDescr,
                                        ProductImagePath  = orderItemData[j].Product.ImagePath,
                                        UnitPrice         = orderItemData[j].OrderItem.UnitPrice,
                                        Quantity          = orderItemData[j].OrderItem.Quantity,
                                        LineTotal         = orderItemData[j].OrderItem.LineTotal
                                    };
                                    vieworders[i].Items.Add(vieworderitem);
                                }
                            }
                        } //order iteration
                        return(vieworders);
                    }     //using ApplicationDbContext
                }
            }             //using DataDbContext
        }
 private void PopulateTaxRates(DataDbContext context)
 {
     context.TaxRates.AddRange(context.DefaultRates);
     context.SaveChanges();
 }