コード例 #1
0
        public async Task <bool> Add <TEntity>(TEntity item) where TEntity : class
        {
            await _db.AddAsync(item);

            //await _db.Set<TEntity>().AddAsync(item);
            int result = await _db.SaveChangesAsync();

            return(result >= 0);
        }
コード例 #2
0
        public async Task <bool> DeleteUser(string userId)
        {
            var dbUser = _db.Users.FirstOrDefault(u => u.Id.Equals(userId));

            if (dbUser == null)
            {
                return(false);
            }
            var userRoles = _db.UserRoles.Where(ur => ur.UserId.Equals(userId));

            _db.UserRoles.RemoveRange(userRoles);
            _db.Users.Remove(dbUser);
            var result = await _db.SaveChangesAsync();

            return(result >= 0);
        }
コード例 #3
0
        public async Task <IActionResult> Create(ProductInventory Input)
        {
            StoreSelectListsInViewData();

            //retrive name details for status message or error message
            var piDetail = GetNameDetails(Input);

            //check if composite primary key already exists in the database
            var pi = _context.ProductInventory.Find(Input.ProductId, Input.StyleId, Input.LocationId);

            if (pi != null)
            {
                ModelState.AddModelError("DuplicateKey", "Duplicate Keys");

                var ErrorMsg = "There is already an inventory entry for " +
                               $"item # <strong>{piDetail.itemNumber}</strong> with style of <strong>{piDetail.style}</strong> at <strong>{piDetail.location}</strong>." +
                               " Please select a different item number, style, and location combination or click " +
                               $@"<a href='/ProductInventories/Edit?ProductId={Input.ProductId}&StyleId={Input.StyleId}&LocationId={Input.StyleId}'>here</a>" +
                               " to edit the existing entry.";
                ViewData["ErrorMsg"]        = ErrorMsg;
                ViewData["htmlStringError"] = new HtmlString(ErrorMsg);
                return(View(Input));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await _context.ProductInventory.AddAsync(Input);

                    int result = await _context.SaveChangesAsync();

                    if (result >= 0)
                    {
                        //Message sent back to Index razor page
                        TempData["StatusMessage"] = $"Created a new inventory entry for Item {piDetail.itemNumber}, Style: {piDetail.style}, Location: {piDetail.location}.";
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            //something failed, redisplay the form
            return(View(Input));
        }