Exemplo n.º 1
0
        public Guid SaveDiscovery(Models.Discovery model, List <Guid> goodsIds)
        {
            bool result;

            using (var dbContext = new DiscoveryDbContext())
            {
                model.LastUpdateTime = DateTime.Now;

                //新增
                if (model.Id == Guid.Empty)
                {
                    model.Id         = KeyGenerator.GetGuidKey();
                    model.CreateTime = DateTime.Now;
                    var currentUser = _userContainer.CurrentUser;
                    model.CreateUserId = currentUser.Id;
                    model.CreateName   = currentUser.UserName;
                    model.Status       = DiscoveryStatus.Normal;
                    dbContext.Discoveries.Add(model);
                }
                else//编辑
                {
                    //删除商品关联
                    var goods = dbContext.DiscoveryGoodsRelations.Where(x => x.DiscoveryId == model.Id).ToList();
                    goods.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted);

                    dbContext.Discoveries.Attach(model);
                    dbContext.Entry(model).State = EntityState.Modified;
                }

                //关联商品
                foreach (var goodsId in goodsIds)
                {
                    DiscoveryGoodsRelation relation = new DiscoveryGoodsRelation
                    {
                        Id          = KeyGenerator.GetGuidKey(),
                        DiscoveryId = model.Id,
                        GoodsId     = goodsId
                    };
                    dbContext.DiscoveryGoodsRelations.Add(relation);
                }

                result = dbContext.SaveChanges() > 0;
            }

            if (!result)
            {
                return(Guid.Empty);
            }

            return(model.Id);
        }
Exemplo n.º 2
0
        public void DeleteDiscovery(Guid id)
        {
            using (var dbContext = new DiscoveryDbContext())
            {
                //删除发现文章
                var discovery = dbContext.Discoveries.FirstOrDefault(x => x.Id == id);
                dbContext.Entry(discovery).State = EntityState.Deleted;
                //删除关联商品
                var goods = dbContext.DiscoveryGoodsRelations.Where(x => x.DiscoveryId == id).ToList();
                goods.ForEach(m => dbContext.Entry(m).State = EntityState.Deleted);

                dbContext.SaveChanges();
            }
        }