public void CreateAuthor() { Console.Write("Enter name: "); string name = Console.ReadLine(); Author a1 = new Author { Author1 = name }; using (MyDBEntities db = new MyDBEntities()) { db.Author.Add(a1); db.SaveChanges(); } ShowAuthors(); }
public void CreateBook() { int id; Console.Write("Enter title: "); string title = Console.ReadLine(); Console.Write("Enter year: "); DateTime year; if (DateTime.TryParse("01-01-" + Console.ReadLine(), out year)) { Book b1 = new Book { Title = title, Year = year }; using (MyDBEntities db = new MyDBEntities()) { db.Book.Add(b1); db.SaveChanges(); } while (true) { Console.Write("Enter id_Book (or letter to exit): "); if (int.TryParse(Console.ReadLine(), out id)) { BookAuthor ba = new BookAuthor { Id_Book = b1.Id_Book, Id_Author = id }; using (MyDBEntities db = new MyDBEntities()) { db.BookAuthor.Add(ba); Author a = db.Author.Find(id); if (a != null) { db.SaveChanges(); } } } else { break; } } } ShowBooks(); }
public void Delete() { int id; Console.Write("Enter id_Book: "); if (int.TryParse(Console.ReadLine(), out id)) { using (MyDBEntities db = new MyDBEntities()) { Book b = db.Book.Find(id); if (b != null) { db.Book.Remove(b); db.SaveChanges(); } } } ShowBooks(); }