public async Task<IActionResult> CreateOrUpdate(OrderItem orderItem) { //flowerHouseContext.Entry(flower).State = (flowerHouseContext.Flowers.Any(f => f.FlowerId == flower.FlowerId) ? EntityState.Modified : EntityState.Added); var updateEntity = await flowerHouseContext.OrderItems.FirstOrDefaultAsync(o => o.OrderItemId == orderItem.OrderItemId); if (updateEntity == null) { updateEntity = orderItem; await flowerHouseContext.AddAsync(updateEntity); } else { updateEntity.Count = orderItem.Count; updateEntity.FlowerId = orderItem.FlowerId; updateEntity.OrderId = orderItem.OrderId; flowerHouseContext.Update(updateEntity); } await flowerHouseContext.SaveChangesAsync(); return RedirectToAction("List"); }
public async Task <IActionResult> CreateOrUpdate(Flower flower, int[] tagid) { var updateFlower = await flowerHouseContext.Flowers.Include(f => f.Stock).FirstOrDefaultAsync(f => f.FlowerId == flower.FlowerId); if (updateFlower == null) { updateFlower = flower; updateFlower.FlowerBuy = -1; //Purchase purchase = new Purchase //{ // Flower = updateFlower, // PurchaseCount = updateFlower.FlowerCount, // PurchasePrice = updateFlower.FlowerCount * updateFlower.FlowerPrice, // PurchaseTime = DateTime.Now //}; //await flowerHouseContext.AddAsync(purchase); await flowerHouseContext.AddAsync(updateFlower); } else { updateFlower.FlowerName = flower.FlowerName; updateFlower.FlowerMaterial = flower.FlowerMaterial; updateFlower.FlowerEmblem = flower.FlowerEmblem; updateFlower.DistributionRange = flower.DistributionRange; updateFlower.FlowerBuy = flower.FlowerBuy; //updateFlower.FlowerPrice = flower.FlowerPrice; updateFlower.FlowerImg = flower.FlowerImg; flowerHouseContext.Update(updateFlower); } updateFlower.FlowerTags = new List <FlowerTag>(); foreach (int tid in tagid) { updateFlower.FlowerTags.Add(new FlowerTag { FlowerId = updateFlower.FlowerId, TagId = tid }); } await flowerHouseContext.SaveChangesAsync(); return(RedirectToAction("List")); }
public async Task <IActionResult> CreateOrUpdate(Order order) { //flowerHouseContext.Entry(flower).State = (flowerHouseContext.Flowers.Any(f => f.FlowerId == flower.FlowerId) ? EntityState.Modified : EntityState.Added); var updateEntity = await flowerHouseContext.Orders.FirstOrDefaultAsync(o => o.OrderId == order.OrderId); if (updateEntity == null) { await flowerHouseContext.AddAsync(order); } else { updateEntity.CreateTime = order.CreateTime; updateEntity.TimeOutTime = order.CreateTime.Value.AddMinutes(30); updateEntity.FinishTime = order.FinishTime; updateEntity.TotalPrice = order.TotalPrice; updateEntity.Address = order.Address; updateEntity.HouseUserId = order.HouseUserId; updateEntity.OrderStatus = order.OrderStatus; flowerHouseContext.Update(updateEntity); } await flowerHouseContext.SaveChangesAsync(); return(RedirectToAction("List")); }