Exemple #1
0
        private void addStudent(string student)
        {
            using (var contex = new LibraryEntities())
            {
                Student newStudent = new Student()
                {
                    Name = student
                };

                try
                {
                    contex.Students.Add(newStudent);
                    contex.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }
Exemple #2
0
 internal void returnBook(int bookId, string student)
 {
     using (var contex = new LibraryEntities())
     {
         var result = contex.BorrowedBooks.SingleOrDefault(bb => bb.BookId == bookId && bb.IsBorrowed == true);
         if (result != null)
         {
             result.IsBorrowed = false;
             contex.SaveChanges();
         }
     }
 }
Exemple #3
0
        private void addBorrowedBookRegister(int bookId, int studentId)
        {
            BorrowedBook borrowBookResgister = new BorrowedBook()
            {
                BookId = bookId, StudentId = studentId, IsBorrowed = true
            };

            using (var contex = new LibraryEntities())
            {
                contex.BorrowedBooks.Add(borrowBookResgister);
                contex.SaveChanges();
            }
        }
Exemple #4
0
        private void registerBorrowBook(int bookId, string studentName)
        {
            using (var contex = new LibraryEntities())
            {
                var result = contex.Books.SingleOrDefault(b => b.Id == bookId);
                if (result != null)
                {
                    result.BorrowedTimes++;
                    contex.SaveChanges();
                }

                var student = contex.Students.SingleOrDefault(s => s.Name == studentName);
                addBorrowedBookRegister(bookId, student.Id);
            }
        }