public List <BookEntity> GetAll() { using (var db = new LibraryDataModel()) { var books = (from b in db.Books select new BookEntity { Id = b.Id, Isbn = b.Isbn, LauchDate = b.LauchDate, Title = b.Title, Authors = (from a in db.Author join ab in db.AuthorBook on b.Id equals ab.BookId into abs from ab in abs.DefaultIfEmpty() where a.Id == ab.AuthorId select new AuthorEntity { Birthdate = a.Birthdate, Email = a.Email, Id = a.Id, LastName = a.LastName, Name = a.Name }).DefaultIfEmpty().ToList() }).ToList(); return(books); } }
public AuthorEntity Get(int id) { using (var db = new LibraryDataModel()) { var author = (from a in db.Author where a.Id == id select new AuthorEntity { Birthdate = a.Birthdate, Email = a.Email, Id = a.Id, LastName = a.LastName, Name = a.Name, Books = (from b in db.Books join ab in db.AuthorBook on a.Id equals ab.AuthorId into abs from ab in abs.DefaultIfEmpty() where b.Id == ab.BookId select new BookEntity { Id = b.Id, Isbn = b.Isbn, LauchDate = b.LauchDate, Title = b.Title }).DefaultIfEmpty().ToList() }).SingleOrDefault(); return(author); } }
public bool Update(int id, BookEntity entry) { var book = MapBook(entry); using (var db = new LibraryDataModel()) { db.Books.Attach(book); db.Entry(entry).State = EntityState.Modified; return(db.SaveChanges() > 0); } }
public bool Update(int id, AuthorEntity entry) { var author = MapAuthor(entry); using (var db = new LibraryDataModel()) { db.Author.Attach(author); db.Entry(entry).State = EntityState.Modified; return(db.SaveChanges() > 0); } }
public bool Insert(BookEntity entry) { var book = MapBook(entry); using (var db = new LibraryDataModel()) { db.Books.Add(book); var success = db.SaveChanges() > 0; if (success) { entry.Id = book.Id; } return(success); } }
public bool Delete(int id) { using (var db = new LibraryDataModel()) { var book = db.Books.Find(id); if (book == null) { return(false); } var authorBook = (from ab in db.AuthorBook where ab.BookId == id select ab).ToList(); authorBook.ForEach(ab => db.AuthorBook.Remove(ab)); db.Books.Remove(book); return(db.SaveChanges() > 0); } }
public bool Insert(AuthorEntity entry) { var author = MapAuthor(entry); using (var db = new LibraryDataModel()) { db.Author.Add(author); var success = db.SaveChanges() > 0; if (success) { entry.Id = author.Id; } return(success); } }