public ActionResult Create([Bind(Include = "DLNumber,Name,FathersName,Address,Pin,BloodGroup,DOB,DeptName,State,IssuingID,DateOfIssue,ValidTill,COV1,COV2")] DLDetails inventory) { if (ModelState.IsValid) { _dbContext.DLDetailss.Add(inventory); _dbContext.SaveChanges(); return(RedirectToAction("Index")); } return(View(inventory)); }
static void Main(string[] args) { // without repository AuthorData author = new AuthorData(); author.Name = "name"; var dlContext = new DLContext(); dlContext.Authors.Add(author); dlContext.SaveChanges(); var users = dlContext.Users.Include(x => x.DepartmentID).ToList(); var user = dlContext.Users.FirstOrDefault(x => x.ID == 1); dlContext.Departments.Where(x => x.ID == user.DepartmentID).Load(); // with repository using (var unitOfWork = new UnitOfWork(new DLContext())) { var user1 = unitOfWork.Users.Get(1); var newUser = new UserData(); newUser.UserName = "******"; unitOfWork.Users.Add(newUser); unitOfWork.Complete(); unitOfWork.Users.Remove(newUser); unitOfWork.Complete(); } }
private async Task <int> ConfirmRequests() { IdentityUser user = await _userManager.FindByNameAsync(Request.HttpContext.User.Identity.Name); string userid = user.Id; int successCount = 0; List <int> bookIds = HttpContext.Session.GetObjectFromJson <List <int> >("BOOK_IDS"); List <int> requests = new List <int>(); for (int i = 0; i < bookIds.Count; i++) { Request req = new Request { BookId = bookIds[i], RequesedOn = DateTime.Now.ToString("d"), UserId = userid }; _DLContext.Requests.Add(req); if (_DLContext.SaveChanges() > 0) { requests.Add(bookIds[i]); successCount++; } } bookIds.RemoveAll(bookId => requests.Contains(bookId)); HttpContext.Session.SetObjectAsJson("BOOK_IDS", bookIds); return(successCount); }
private bool IssueBookToUser(int Id) { Boolean retVal = false; using (var dbContextTransaction = db.Database.BeginTransaction()) { try { //Get the request Request BookReq = db.Requests.Single(req => req.RequestID == Id); if (BookReq != null) { string issueDate = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year; //Create an Issue for the request DateTime dueOn = DateTime.Parse(issueDate).AddDays(14); string dueDate = dueOn.Day + "/" + dueOn.Month + "/" + dueOn.Year; Issue Issue = new Issue() { BookId = BookReq.BookId, UserId = BookReq.UserId, IssuedOn = issueDate, DueOn = dueDate }; if (db.Issues.Add(Issue) != null) { Book ReqBook = db.Books.Single(book => book.BookId == BookReq.BookId); if (ReqBook != null) { db.Books.Attach(ReqBook); ReqBook.AvailableCopies--; //If the issue is successfully added, remove the request if (db.Requests.Remove(BookReq) != null) { db.SaveChanges(); retVal = true; } } } } if (retVal) { dbContextTransaction.Commit(); } } catch { dbContextTransaction.Rollback(); retVal = false; } } return(retVal); }
private async Task <bool> ReturnBookFromUser(int Id) { Boolean retVal = false; using (var dbContextTransaction = db.Database.BeginTransaction()) { try { //Get the request Issue issue = await db.Issues.SingleAsync(isue => isue.IssueID == Id); if (issue != null) { //Create an Issue for the request if (db.Issues.Remove(issue) != null) { Book book = await db.Books.SingleAsync(bk => bk.BookId == issue.BookId); if (book != null) { db.Books.Attach(book); book.AvailableCopies++; db.SaveChanges(); retVal = true; } } } if (retVal) { dbContextTransaction.Commit(); } } catch (Exception ex) { dbContextTransaction.Rollback(); retVal = false; } } return(retVal); }
public int Complete() { return(_context.SaveChanges()); }