Ejemplo n.º 1
0
        public IActionResult DeleteUser(int id)
        {
            var u   = dbContext.users.Find(id);
            var doc = dbContext.documents.Where(x => x.UserID == id).ToList();

            foreach (var d in doc)
            {
                var df = dbContext.documentFiles.Where(x => x.document_file_id == d.DocumentFileID).FirstOrDefault();
                df.numberOf_document--;
                var deletedDoc = new DeletedDocument();
                deletedDoc.deleted_document_name = d.document_name;
                deletedDoc.deleted_document_type = d.document_type;
                deletedDoc.UserID = d.UserID;
                dbContext.deletedDocuments.Add(deletedDoc);
                dbContext.Remove(d);
                dbContext.SaveChanges();
            }
            dbContext.users.Remove(u);
            dbContext.SaveChanges();
            TempData.Add("Alert", "User was deleted");
            return(RedirectToAction("Index"));
        }