Example #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);
        }
Example #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();
            }
        }
Example #3
0
        public async Task <int> Modify(Comment model, params string[] proNames)
        {
            discoveryDbContext.Set <Comment>().Attach(model);
            //将对象添加到EF中
            DbEntityEntry entry = discoveryDbContext.Entry <Comment>(model);

            //先设置对象的包装 状态为Unchanged
            entry.State = EntityState.Unchanged;
            //将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
            foreach (string proName in proNames)
            {
                //将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新
                entry.Property(proName).IsModified = true;
            }
            return(await discoveryDbContext.SaveChangesAsync());
        }