Beispiel #1
0
        //phuong thuc insert into table user
        public UserEntity Save(UserEntity userEntity)
        {
            if (userEntity.Id == 0)
            {
                if (getOneByEmail(userEntity.Email) == null)
                {
                    Console.WriteLine("Them moi");
                    dbContext.users.Add(userEntity);
                    dbContext.SaveChanges();
                    return(userEntity);
                }
                else
                {
                    Console.WriteLine("Email đăng kí đã tồn tại trong hệ thống!");
                    return(null);
                }
            }
            else
            {
                Console.WriteLine("Cap nhat");
                dbContext.users.Where(x => x.Id == userEntity.Id).AsNoTracking();
                var local = dbContext.Set <UserEntity>()
                            .Local
                            .FirstOrDefault(entry => entry.Id.Equals(userEntity.Id));
                //check local UserRole
                var localUR = dbContext.Set <UserRole>().Local.ToList <UserRole>();
                foreach (UserRole ur in localUR)
                {
                    Console.WriteLine(ur.Id + ", " + ur.Role.Id + ", " + ", " + ur.User.Id);
                    var localRole = dbContext.Set <RoleEntity>()
                                    .Local
                                    .FirstOrDefault(entry => entry.Id == ur.Role.Id);
                    if (localRole != null)
                    {
                        //detach
                        dbContext.Entry(localRole).State = EntityState.Detached;
                        Console.WriteLine("Detached role is success");
                    }
                }

                // check if local is not null
                if (local != null)
                {
                    // detach
                    dbContext.Entry(local).State = EntityState.Detached;
                }
                dbContext.users.Update(userEntity);
                dbContext.SaveChanges();
                return(userEntity);
            }
        }
        //phương thức cập nhật hình ảnh sản phẩm
        public void UpdateImageProduct(ImageProduct imageProduct, Product2 product, long id)
        {
            Console.WriteLine("imageId={0},productId={1},id={2}", imageProduct.Id, product.Id, id);
            int I = product.Images.Count;

            if (imageProduct.Product.Id == product.Id && imageProduct.Id == id)
            {
                dbContext.Entry(imageProduct).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }
Beispiel #3
0
        public OrderEntity getOrder(OrderEntity cartEntity)
        {
            dbContext.Entry(cartEntity).State = EntityState.Detached;

            return(getOrder(cartEntity.UserId, cartEntity.Id));
        }
Beispiel #4
0
        public CartEntity getCart(CartEntity cartEntity)
        {
            dbContext.Entry(cartEntity).State = EntityState.Detached;

            return(getCart(cartEntity.UserId));
        }