private static void AddUser(string firstName, string lastName, string pseudonim, string email, int phone) { using (var context = new BookDBDataContext()) { if (context.Users.FirstOrDefault(user => user.Email == email) == null) { User temp = new User() { FirstName = firstName, LastName = lastName, Pseudonim = pseudonim, Email = email, Phone = phone }; context.Users.InsertOnSubmit(temp); context.SubmitChanges(); } } }
private static void AddGenresToBook(int bookID, List<string> genres) { using (BookDBDataContext context = new BookDBDataContext()) { foreach (var genre in genres) { AddGenre(genre); var bookGenres = new BookGenre() { BookID = bookID, GenreID = GetGenre(genre).ID }; context.BookGenres.InsertOnSubmit(bookGenres); } context.SubmitChanges(); } }
//private static Author GetAuthor(Author author) //{ // using (var context = new BookDBDataContext()) // { // return context.Authors.FirstOrDefault(a => a.FirstName.Equals(author.FirstName) && a.LastName.Equals(author.LastName)); // } //} private static void AddGenre(string genre) { using (var context = new BookDBDataContext()) { if (context.Genres.FirstOrDefault(g => g.Name.Equals(genre)) == null) { Genre gen = new Genre() { Name = genre }; context.Genres.InsertOnSubmit(gen); context.SubmitChanges(); } } }
private static Book AddBook(string title, string description, DateTime datePublished, int pages, string isbn, int bookCopies) { using (BookDBDataContext context = new BookDBDataContext()) { Book newBook = new Book() { Title = title, BookResume = description, DatePublished = datePublished, Pages = pages, ISBN = isbn }; context.Books.InsertOnSubmit(newBook); while (bookCopies > 0) { var bookCopy = new BookCopy() { Book = newBook }; context.BookCopies.InsertOnSubmit(bookCopy); bookCopies--; } //var existing = context.Authors.FirstOrDefault(a => a.FirstName.Contains("G")); //if (existing != null) //{ // var bookAuthor = new BookAuthor() // { // BookID = newBook.ID, // AuthorID = existing.ID // }; //} //var bookGenre = new BookGenre() //{ // BookID = newBook.ID //}; context.SubmitChanges(); return newBook; } }
private static void AddAuthorsToBook(int bookID, List<Author> bookAuthors) { using (BookDBDataContext context = new BookDBDataContext()) { foreach (var author in bookAuthors) { var bookAuthor = new BookAuthor() { BookID = bookID, AuthorID = author.ID }; context.BookAuthors.InsertOnSubmit(bookAuthor); } context.SubmitChanges(); } }
private static void AddAuthor(string firstName, string lastName, DateTime yearBorn, DateTime yearDied) { using (var context = new BookDBDataContext()) { if (context.Authors.FirstOrDefault(author => author.FirstName == firstName && author.LastName == lastName) == null) { Author temp = new Author() { FirstName = firstName, LastName = lastName, YearBorn = yearBorn, YearDied = yearDied }; context.Authors.InsertOnSubmit(temp); context.SubmitChanges(); } } }