public static Order GetOrder(string id)
 {
     using (var db = new OrderContexts())
     {
         return(AllOrders(db).FirstOrDefault(o => o.Id == id));
     }
 }
 public static List <Customer> GetAll()
 {
     using (var db = new OrderContexts())
     {
         return(db.Customers.ToList());
     }
 }
 public static List <Order> GetAllOrders()
 {
     using (var db = new OrderContexts())
     {
         return(AllOrders(db).ToList());
     }
 }
 public static List <Order> QueryOrdersByCustomerName(string customerName)
 {
     using (var db = new OrderContexts())
     {
         var query = AllOrders(db)
                     .Where(o => o.Customer.Name == customerName);
         return(query.ToList());
     }
 }
 public static List <Order> QueryOrdersByGoodsName(string goodsName)
 {
     using (var db = new OrderContexts())
     {
         var query = AllOrders(db)
                     .Where(o => o.Items.Count(i => i.GoodsItem.Name == goodsName) > 0);
         return(query.ToList());
     }
 }
 public static object QueryByTotalAmount(float amout)
 {
     using (var db = new OrderContexts())
     {
         return(AllOrders(db)
                .Where(o => o.Items.Sum(item => item.GoodsItem.Price * item.Quantity) > amout)
                .ToList());
     }
 }
 private static void RemoveItems(string orderId)
 {
     using (var db = new OrderContexts())
     {
         var oldItems = db.OrderItems.Where(item => item.OrderId == orderId);
         db.OrderItems.RemoveRange(oldItems);
         db.SaveChanges();
     }
 }
 public static void UpdateOrder(Order newOrder)
 {
     RemoveItems(newOrder.Id);
     using (var db = new OrderContexts())
     {
         db.Entry(newOrder).State = EntityState.Modified;
         db.OrderItems.AddRange(newOrder.Items);
         db.SaveChanges();
     }
 }
 //获取对象,进行更改,保存更改,EF框架会更改对应的数据项
 public static void Add(Goods goods)
 {
     try {
         using (var db = new OrderContexts()) {
             db.GoodItems.Add(goods);
             db.SaveChanges();
         }
     }
     catch (Exception e) {
         throw new ApplicationException($"添加错误!");
     }
 }
Beispiel #10
0
 public static void Main()
 {
     try {
         using (var db = new OrderContexts())
         {
             //绑定
             var order = db.OrderItems.FirstOrDefault();
         }
     }catch (Exception e) {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.StackTrace);
     }
 }
 public static Order AddOrder(Order order)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             db.Orders.Add(order);
             db.SaveChanges();
         }
         return(order);
     }
     catch (Exception e)
     {
         throw new ApplicationException($"添加错误: {e.Message}");
     }
 }
 public static void Add(Customer customer)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             //获取对象,进行更改,保存更改,EF框架会更改对应的数据项
             db.Customers.Add(customer);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new ApplicationException($"添加错误!");
     }
 }
 public static void RemoveOrder(string id)
 {
     try
     {
         using (var db = new OrderContexts())
         {
             var order = db.Orders.Include("Items").Where(o => o.Id == id).FirstOrDefault();
             db.Orders.Remove(order);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         //TODO 需要更加错误类型返回不同错误信息
         throw new ApplicationException($"删除订单错误!");
     }
 }
        /* public List<Order> Orders {
         * get => orders;
         * }
         */
        //

        private static IQueryable <Order> AllOrders(OrderContexts db)
        {
            return(db.Orders.Include(o => o.Items.Select(i => i.GoodsItem))
                   .Include("Customer"));
        }
 public static List <Goods> GetAll()
 {
     using (var db = new OrderContexts()) {
         return(db.GoodItems.ToList());
     }
 }