public List <Book> GetAllBooks() { using (var context = new BooksEntities()) { return(context.Books.ToList().Select(b => Mapper.Map <Book>(b)).ToList()); } }
private static List<Author> ListAuthors() { using (var db = new BooksEntities()) { List<Author> list = db.Authors.ToList(); return list; } }
private static Author SearchAuthorId(int id) { using (var db = new BooksEntities()) { var name = db.Authors.Find(id); return name; } }
public IHttpActionResult GetFinderBooks(BooksEntities booksEntities) { using (DBNexosBook db = new DBNexosBook()) { var a = BooksDAL.FinderBook(db, booksEntities); booksEntities = a.FirstOrDefault(); } return(Ok(booksEntities)); }
public List <Set> GetAllSets() { using (var context = new BooksEntities()) { var res = context.Sets.Include(s => s.BooksSetsBindings.Select(b => b.Books)).ToList(); return(res.Select(s => Mapper.Map <SetDb, Set>(s)).ToList()); } }
public void Delete(decimal id) { using (var data = new BooksEntities()) { var my = (from e in data.book select e).Where(z => z.Id.Equals(id)).FirstOrDefault(); data.book.Remove(my); data.SaveChanges(); } }
public List <Category> GetAllCategories() { using (var context = new BooksEntities()) { var res = context.Categories.Include(s => s.BooksCategoriesBindings.Select(b => b.Books)).ToList(); return(res.Select(s => Mapper.Map <CategoryDb, Category>(s)).ToList()); } }
private static List<Author> SearchAuthor(string y) { using (var db = new BooksEntities()) { List<Author> names = (from x in db.Authors where x.FirstName.Contains(y) || x.LastName.Contains(y) select x).ToList(); return names; } }
public void Add(Book book) { using (var context = new BooksEntities()) { var newEnt = Mapper.Map <BookDb>(book); context.Books.Add(newEnt); context.SaveChanges(); } }
static void Main(string[] args) { SetDir(); using (var ctx = new BooksEntities()) { List <Author> authors = ctx.Authors.ToList(); foreach (Author author in authors) { author.SSN = RandomString(9); } ctx.SaveChanges(); } }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { BooksEntities dbcontext = new BooksEntities(); var titleAndAuthors = from title in dbcontext.Titles from author in title.Authors orderby title.Title1 select new { title.Title1, author.FirstName, author.LastName }; Result += "Titles and Authors:"; foreach (var element in titleAndAuthors) { Result += String.Format("\r\n\t{0,-10} {1} {2}", element.Title1, element.FirstName, element.LastName); } // end foreach var authorsAndTitles = from book in dbcontext.Titles from author in book.Authors orderby book.Title1, author.LastName, author.FirstName select new { author.FirstName, author.LastName, book.Title1 }; Result += "\r\n\r\nAuthors and titles with authors sorted for each title:"; foreach (var element in authorsAndTitles) { Result += String.Format("\r\n\t{0,-10} {1} {2}", element.Title1, element.FirstName, element.LastName); } // end foreach var titlesByAuthor = from book in dbcontext.Titles orderby book.Title1 select new { Title = book.Title1, AuthorNames = from author in book.Authors orderby author.LastName, author.FirstName select author.FirstName + " " + author.LastName }; Result += "\r\n\r\nTitles grouped by author:"; foreach (var book in titlesByAuthor) { Result += "\r\n\t" + book.Title + ":"; foreach (var Name in book.AuthorNames) { Result += "\r\n\t\t" + Name; } } }
public static BookVM BookToVM(Book book) { BooksEntities db = new BooksEntities(); BookVM bookVM = new Models.BookVM(); bookVM.BookID = book.Id; bookVM.Name = book.Name; bookVM.Points = book.Points; bookVM.Discription = book.Discription; if (book.City_Id == null) { bookVM.CityId = null; } else { bookVM.CityId = book.City_Id; bookVM.CityName = book.City.Name; } bookVM.Date_Uploaded = book.Date_Uploaded; bookVM.Subcategory_Id = book.Subcategory_Id; bookVM.Images = new List <string>(); foreach (var img in book.Images) { bookVM.Images.Add(img.Img_URL); } var subcatName = (from c in db.SubCategories where c.Id == book.Subcategory_Id select c.Name).FirstOrDefault(); bookVM.SubCategory = subcatName; var booksIDs = (from b in db.Seller_Approved_book where b.Approved == "Yes" && b.Book_Id == book.Id select b).FirstOrDefault(); if (booksIDs != null) { bookVM.UserID = booksIDs.User.Id; bookVM.User_Name = booksIDs.User.UserName; } return(bookVM); }
private void QueryTableForm_Load(object sender, EventArgs e) { BooksEntities db = new BooksEntities(); var authorsAndTitles = from book in db.Titles from author in book.Authors select new { author.FirstName, author.LastName, book.Title }; outputTxt.AppendText("Authors and Titles:"); foreach (var element in authorsAndTitles) { outputTxt.AppendText(String.Format("\r\n\t{0,-10} {1,-10} {2}", element.FirstName, element.LastName, element.Title)); } }
public void Update(Book book) { using (var context = new BooksEntities()) { var bookToUpdate = context.Books.SingleOrDefault(b => b.ID == book.Id); if (bookToUpdate != null) { Mapper.Map(book, bookToUpdate); context.SaveChanges(); } else { throw new DBConcurrencyException(); } } }
public void update(BookModel a) { using (var data = new BooksEntities()) { var my = (from d in data.book where d.Id == a.id select d).FirstOrDefault(); my.Name = a.Name; my.author = a.author; my.editorial = a.editorial; my.edition = a.edition; my.publication = a.publication; my.id_city = a.id_city; my.Country = a.Country; data.SaveChanges(); } }
public void Remove(int id) { using (var context = new BooksEntities()) { var bookToRemove = context.Books.SingleOrDefault(b => b.ID == id); if (bookToRemove != null) { context.Entry(bookToRemove).State = EntityState.Deleted; context.SaveChanges(); } else { throw new DBConcurrencyException(); } } }
//to create a record public void Create(BookModel u) { using (var data = new BooksEntities()) { book bok = new book(); bok.Name = u.Name; bok.author = u.author; bok.editorial = u.editorial; bok.edition = u.edition; bok.publication = u.publication; bok.id_city = u.id_city; bok.Country = u.Country; data.book.Add(bok); data.SaveChanges(); } }
public static string AddBook(DBNexosBook db, BooksEntities books) { if (books.Title == null || books.Gender == null) { return("Los datos no son correctos"); } AuthorsController authorsController = new AuthorsController(); EditorialsController editorialsController = new EditorialsController(); var author = db.Authors.Where(x => x.Name == books.Author).FirstOrDefault(); if (author == null) { return("El autor no está registrado"); } var editorial = db.Editorials.Where(x => x.Name == books.Editorial).FirstOrDefault(); if (editorial == null) { return("La editorial no está registrada"); } if (editorial.CurrenBooks >= editorial.MaxBooks) { return("La editorial no tiene capacidad para mas libros"); } Books bookUpdate = new Books(); bookUpdate.Title = books.Title; bookUpdate.CreationDate = books.CreationDate; bookUpdate.Gender = books.Gender; bookUpdate.NumPages = books.NumPages; bookUpdate.Id_Author = author.Id_Author; bookUpdate.Id_Editorial = editorial.Id_Editorial; db.Books.Add(bookUpdate); db.SaveChanges(); editorial.CurrenBooks += 1; editorialsController.PutEditorials(editorial.Id_Editorial, editorial); return("El libro fue registrado con exito"); }
public BookModel Consultbo(decimal Id) { BookModel re = new BookModel(); using (var data = new BooksEntities()) { var my = (from c in data.book select c).Where(z => z.Id.Equals(Id)).FirstOrDefault(); re.id = my.Id; re.Name = my.Name; re.author = my.author; re.editorial = my.editorial; re.edition = my.edition; re.publication = my.publication; re.id_city = my.id_city; re.Country = my.Country; } return(re); }
public string PostBooks(BooksEntities books) { string response; try { using (DBNexosBook db = new DBNexosBook()) { response = BooksDAL.AddBook(db, books); } } catch (Exception) { return("No se pudo hacer el registro, consulte a su administrador"); throw; } return(response); }
private static void DisplayAuthorsAndISBNs(BooksEntities db) { var authorsAndISBNs = from author in db.Authors from title in author.Titles orderby author.LastName, author.FirstName select new { author.FirstName, author.LastName, title.ISBN }; var buf = new StringBuilder(); // display authors and ISBNs in tabular format foreach (var element in authorsAndISBNs) { buf.Append($"\r\n\t{element.FirstName,-10} " + $"{element.LastName,-10} {element.ISBN,-10}"); } Console.WriteLine(buf.ToString()); }
private static void Main(string[] args) { var r = new Random(); using (var db = new BooksEntities()) { var authors = db.Authors; authors.Add(new Author { FirstName = "Damon", LastName = "German", Titles = new List <Title> { new Title { Title1 = "Hello World", Copyright = "2018", EditionNumber = 1, ISBN = GetRandomString(r, 15) } } }); db.SaveChanges(); foreach (var author in authors) { author.FirstName = author.FirstName.ToUpper(); author.LastName = author.LastName.ToUpper(); } db.SaveChanges(); DisplayAuthorsAndISBNs(db); DisplayAuthorsAndTitles(db); } }
public AuthorsQuery() { Field <AuthorsType>( "authors", //arguments: new QueryArguments(new QueryArgument<StringGraphType> { Name = "name" }), resolve: context => { //var name = context.GetArgument<string>("name"); using (var db = new BooksEntities()) { var list = (from x in db.Authors select x).ToList().FirstOrDefault(); list.Books.ToList(); var type = list.Books.GetType(); foreach (var props in list.Books) { props.Genres.ToList(); } return(list); } } ); }
public List <BookModel> Consult() { List <BookModel> result = new List <BookModel>(); using (var data = new BooksEntities()) { var resultconsult = (from d in data.book select d).ToList(); foreach (var item in resultconsult) { BookModel x = new BookModel(); x.id = item.Id; x.Name = item.Name; x.author = item.author; x.editorial = item.editorial; x.edition = item.edition; x.publication = item.publication; x.id_city = item.id_city; x.Country = item.Country; result.Add(x); } } return(result); }
private void comboBoxQuery_SelectedIndexChanged(object sender, EventArgs e) { BooksEntities dbcontext = new BooksEntities(); txtQueryResult.Clear(); // clear the current content of the textbox switch (comboBoxQuery.SelectedIndex) { case 0: // get authors of each book they co-authored, sorted by title var booksAndAuthorsOrderedByTitle = from book in dbcontext.Titles from author in book.Authors orderby book.Title1 select new { book.Title1, Name = author.LastName + " " + author.FirstName }; txtQueryResult.AppendText("TITLES AND AUTHORS (sorted by title): "); foreach (var element in booksAndAuthorsOrderedByTitle) { txtQueryResult.AppendText($"\r\n\r\n\t \"{element.Title1,-10}\", " + $"co-authored by {element.Name,-10}"); } break; case 1: // get authors of each book they co-authored, sorted by title, author's last name, author's first name var booksAndAuthorsOrderedByTitleLastFirst = from book in dbcontext.Titles from author in book.Authors orderby book.Title1, author.LastName, author.FirstName select new { book.Title1, Name = author.LastName + " " + author.FirstName }; txtQueryResult.AppendText("TITLES AND AUTHORS (sorted by title, author's last name, author's first name): "); foreach (var element in booksAndAuthorsOrderedByTitleLastFirst) { txtQueryResult.AppendText($"\r\n\r\n\t \"{element.Title1,-10}\"" + $"co-authored by {element.Name,-10}"); } break; case 2: // get authors of each book, group them by the book title - sorted by title, author's last name, author's first name var authorsGroupedByBookOrderedByTitleLastFirst = from book in dbcontext.Titles orderby book.Title1 select new { book.Title1, ItsAuthorsLastName = (from author in book.Authors orderby author.LastName, author.LastName select new { author.LastName, author.FirstName }) }; txtQueryResult.AppendText("TITLES AND AUTHORS " + "(sorted and grouped by title; sorted by author's last name, then author's first name): "); foreach (var element in authorsGroupedByBookOrderedByTitleLastFirst) { txtQueryResult.AppendText($"\r\n\r\n\t \"{element.Title1}\", by"); foreach (var author in element.ItsAuthorsLastName) { txtQueryResult.AppendText($"\r\n\r\n\t\t {author.LastName} {author.FirstName}"); } } break; } }
public BookRepository(BooksEntities context) { this.context = context; }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { // Entity Framework DBContext BooksEntities dbcontext = new BooksEntities(); //get authors and and titles, sorted by title var TitlesAndAuthors = from author in dbcontext.Authors from book in author.Titles orderby book.Title1 select new { book.Title1, author.FirstName, author.LastName }; outputTextBox += ("Titles and Authors:"); //display authors and and titles, sorted by foreach (var element in TitlesAndAuthors) { outputTextBox += ( String.Format("\r\n\t{0,-50} {1} {2}", element.Title1, element.FirstName, element.LastName)); } // end foreach outputTextBox += ("\r\n\r\n"); //get authors and titles, sorted by authors var byAuthors = from author in dbcontext.Authors from book in author.Titles orderby book.Title1, author.FirstName, author.LastName select new { book.Title1, author.FirstName, author.LastName }; outputTextBox += ("Authors and titles with authors sorted for each title:"); //display orderd by authors for each book. foreach (var element in byAuthors) { outputTextBox += ( String.Format("\r\n\t{0,-50} {1} {2}", element.Title1, element.FirstName, element.LastName)); } // end foreach // get authors and titles of each book // they co-authored; group by author var authorsByTitle = from book in dbcontext.Titles orderby book.Title1 select new { Title = book.Title1, Authors = from author in book.Authors orderby author.LastName, author.FirstName select author.FirstName + " " + author.LastName }; outputTextBox += ("\r\n\r\nTitles grouped by author:"); // display titles written by each author, grouped by author foreach (var Titles in authorsByTitle) { // display author's name outputTextBox += ("\r\n\t" + Titles.Title + ":"); foreach (var Author in Titles.Authors) { outputTextBox += ("\r\n\t" + Author); } } // end outer foreach this.RaisePropertyChanged(() => outputTextBox); }
private void QueryCmb_SelectedIndexChanged(object sender, EventArgs e) { switch (queryCmb.SelectedIndex) { case 0: outputTxt.Text = String.Empty; BooksEntities db = new BooksEntities(); var sortByTitle = from book in db.Titles from author in book.Authors orderby book.Title select new { author.FirstName, author.LastName, book.Title }; outputTxt.AppendText("Authors and Titles:"); foreach (var element in sortByTitle) { outputTxt.AppendText(String.Format("\r\n\t{0,-10} {1,-10} {2}", element.FirstName, element.LastName, element.Title)); } break; case 1: outputTxt.Text = String.Empty; db = new BooksEntities(); var sortByTitleAndName = from book in db.Titles from author in book.Authors orderby book.Title, author.LastName, author.FirstName select new { author.FirstName, author.LastName, book.Title }; outputTxt.AppendText("Authors and Titles:"); foreach (var element in sortByTitleAndName) { outputTxt.AppendText(String.Format("\r\n\t{0,-10} {1,-10} {2}", element.FirstName, element.LastName, element.Title)); } break; case 2: outputTxt.Text = String.Empty; db = new BooksEntities(); var groupByTitle = from book in db.Titles orderby book.Title select new { Titles = book.Title, Names = from author in book.Authors orderby author.LastName, author.FirstName select author.LastName + author.FirstName }; outputTxt.AppendText("Authors grouped by title:"); foreach (var title in groupByTitle) { outputTxt.AppendText(String.Format("\r\n\t{0,-10}", title.Titles )); foreach (var name in groupByTitle) { outputTxt.AppendText(String.Format("\r\n\t{0,-10}", name.Names )); } } break; } }
static void Test() { var parser = new GenericParserAdapter( Path.Combine(new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName, "Data", "500.csv")); parser.FirstRowHasHeader = true; DataTable t = parser.GetDataTable(); foreach (DataRow r in t.Rows) { using (var ctx = new BooksEntities()) { string sm = r["State"].ToString(); List <State> states = ctx.States.Where(s => s.ShortName.Equals(sm)).ToList(); State state = null; if (states.Count > 0) { state = states[0]; } if (state == null) { continue; } string cm = r["City"].ToString(); List <City> cities = ctx.Cities.Where(c => c.Name.Equals(cm)).ToList(); City city = null; if (cities.Count > 0) { city = cities[0]; } if (city == null) { City newCity = new City(); newCity.Name = cm; newCity.StateId = state.StateId; city = ctx.Cities.Add(newCity); } string zcc = r["ZIP"].ToString(); List <ZipCode> zcs = ctx.ZipCodes.Where(z => z.ZipCode1.Equals(zcc)).ToList(); ZipCode zc = null; if (zcs.Count > 0) { zc = zcs[0]; } if (zc == null) { ZipCode newzc = new ZipCode(); newzc.ZipCode1 = zcc; newzc.CityId = city.CityId; zc = ctx.ZipCodes.Add(newzc); } string name = r[0].ToString() + " " + r[1].ToString(); List <Author> authors = ctx.Authors.Where(a => a.Name.Equals(name)).ToList(); Author author = null; if (authors.Count > 0) { author = authors[0]; } if (author == null) { Author newAuthor = new Author(); newAuthor.Name = name; newAuthor.SSN = RandomString(11); newAuthor.ZipCodeId = zc.ZipCodeId; author = ctx.Authors.Add(newAuthor); } ctx.SaveChanges(); } } }
public static List <BooksEntities> FinderBook(DBNexosBook db, BooksEntities booksEntities) { if (booksEntities.Author != null) { var ret = from B in db.Books join E in db.Editorials on B.Id_Editorial equals E.Id_Editorial join A in db.Authors on B.Id_Author equals A.Id_Author where A.Name == booksEntities.Author select new BooksEntities { Title = B.Title, CreationDate = B.CreationDate, Gender = B.Gender, Author = A.Name, Editorial = E.Name }; return(ret.ToList()); } if (booksEntities.Editorial != null) { var ret = from B in db.Books join E in db.Editorials on B.Id_Editorial equals E.Id_Editorial join A in db.Authors on B.Id_Author equals A.Id_Author where E.Name == booksEntities.Editorial select new BooksEntities { Title = B.Title, CreationDate = B.CreationDate, Gender = B.Gender, Author = A.Name, Editorial = E.Name }; return(ret.ToList()); } if (booksEntities.Title != null) { var ret = from B in db.Books join E in db.Editorials on B.Id_Editorial equals E.Id_Editorial join A in db.Authors on B.Id_Author equals A.Id_Author where B.Title == booksEntities.Title select new BooksEntities { Title = B.Title, CreationDate = B.CreationDate, Gender = B.Gender, Author = A.Name, Editorial = E.Name }; return(ret.ToList()); } if (booksEntities.CreationDate != null) { var ret = from B in db.Books join E in db.Editorials on B.Id_Editorial equals E.Id_Editorial join A in db.Authors on B.Id_Author equals A.Id_Author where B.CreationDate == booksEntities.CreationDate select new BooksEntities { Title = B.Title, CreationDate = B.CreationDate, Gender = B.Gender, Author = A.Name, Editorial = E.Name }; return(ret.ToList()); } return(null); }
public BooksData() { booksEntities = new BooksEntities(); }
private static Author DeleteAuthorAuthor(Author author) { using (var db = new BooksEntities()) { var oldAuthor = db.Authors.Find(author.AuthorID); db.Authors.Remove(oldAuthor); db.SaveChanges(); return author; } }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { BooksEntities dbcontext = new BooksEntities(); //Get a list of all the titles and the authors who wrote them. //Sort the result by title. var allTitles = from author in dbcontext.Authors from book in author.Titles orderby book.Title1 select new { author.FirstName, author.LastName, book.Title1 }; Text += "Titles and Authors:"; // display titles and author foreach (var element in allTitles) { Text += "\r\n\t" + element.Title1 + "\t" + element.FirstName + "\t" + element.LastName; } // end foreach //Get a list of all the titles and the authors who wrote them. //Sort the result by title. //For each title sort the authors alphabetically by last name, then first name. var allTitlesLF = from author in dbcontext.Authors from book in author.Titles orderby book.Title1, author.LastName, author.FirstName select new { author.FirstName, author.LastName, book.Title1 }; Text += "\r\n\r\nAuthors and titles with authors sorted for each title:"; // display titles and author foreach (var element in allTitlesLF) { Text += "\r\n\t" + element.Title1 + "\t" + element.LastName + "\t" + element.FirstName; } // end foreach //Get a list of all the authors grouped by title, sorted by title //for a given title sort the author names alphabetically by last name first then first name. var authorsByTitle = from author in dbcontext.Authors from book in author.Titles group new { author.FirstName, author.LastName } by book.Title1 into bookGroup orderby bookGroup.Key select bookGroup; Text += "\r\n\r\nTitles grouped by author:"; // display titles written by each author, grouped by titles foreach (var title in authorsByTitle) { // display titles's name Text += "\r\n\t" + title.Key + ":"; // display authors foreach (var author in title.OrderBy(name => name.LastName + name.FirstName)) { Text += "\r\n\t\t" + author.FirstName + " " + author.LastName; } // end inner foreach } // end outer foreach this.RaisePropertyChanged(() => this.Text); }
private static Author UpdateAge(Author author, int age) { using (var db = new BooksEntities()) { var oldAuther = db.Authors.Find(author.AuthorID); oldAuther.Age = age; db.SaveChanges(); return oldAuther; } }
public IHttpActionResult Delete(int id) { bool selll = false; if (id <= 0) { return(BadRequest("Not a valid Book id")); } var selby = (from i in db.Seller_Buyer_Book select i.Book_Id).ToList(); foreach (var item in selby) { if (item == id) { selll = true; } } if (selll) { return(BadRequest("Not a valid Book id")); } else { using (var ctx = new BooksEntities()) { var bookrequest = ctx.Requests .Where(s => s.Book_Id == id) .FirstOrDefault(); if (bookrequest != null) { ctx.Entry(bookrequest).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } var bookapproved = ctx.Seller_Approved_book .Where(s => s.Book_Id == id) .FirstOrDefault(); if (bookapproved != null) { ctx.Entry(bookapproved).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } var imglist = (from i in db.Images where i.Book_Id == id select i.Id).ToList(); foreach (var item in imglist) { var img = db.Images.Where(e => e.Id == item).First(); if (img != null) { db.Images.Remove(img); db.SaveChanges(); } } var book = ctx.Books .Where(s => s.Id == id) .FirstOrDefault(); ctx.Entry(book).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } } return(Ok()); }
private static Author AddAuthor(string firstName, string lastName) { using (var db = new BooksEntities()) { Author author = new Author(); author.FirstName = firstName; author.LastName = lastName; db.Authors.Add(author); db.SaveChanges(); return author; } }
private static Author UpdateAuthor(Author author) { using (var db = new BooksEntities()) { var oldAuthor = db.Authors.Find(author.AuthorID); oldAuthor.FirstName = author.FirstName; oldAuthor.LastName = author.LastName; db.SaveChanges(); return author; } }
public IHttpActionResult Put(AddbooVM editbook) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } bool govexsist = false; bool cityexsist = false; int govid; int citid; var govnames = (from i in db.Governates select i.Name).ToList(); foreach (var item in govnames) { if (item == editbook.Governate) { govexsist = true; } } if (govexsist) { govid = (from i in db.Governates where i.Name == editbook.Governate select i.Id).First(); } else { Governate Gov = new Governate(); Gov.Name = editbook.Governate; db.Governates.Add(Gov); db.SaveChanges(); govid = (from i in db.Governates where i.Name == editbook.Governate select i.Id).First(); } var citynames = (from i in db.Cities select i.Name).ToList(); foreach (var item in citynames) { if (item == editbook.City) { cityexsist = true; } } if (cityexsist) { citid = (from i in db.Cities where i.Name == editbook.City select i.Id).First(); } else { City cit = new City(); cit.Name = editbook.City; cit.Governate_Id = govid; db.Cities.Add(cit); db.SaveChanges(); citid = (from i in db.Cities where i.Name == editbook.City select i.Id).First(); } var subid = (from i in db.SubCategories where i.Name == editbook.SubCatogry select i.Id).First(); var imglist = (from i in db.Images where i.Book_Id == editbook.BookId select i.Id).ToList(); foreach (var item in imglist) { var img = db.Images.Where(e => e.Id == item).First(); db.Images.Remove(img); db.SaveChanges(); } using (var ctx = new BooksEntities()) { var existingbook = ctx.Books.Where(s => s.Id == editbook.BookId) .FirstOrDefault <Book>(); if (existingbook != null) { existingbook.Name = editbook.BookName; existingbook.Points = editbook.Points; existingbook.Discription = editbook.Discription; existingbook.City_Id = citid; existingbook.Subcategory_Id = subid; ctx.SaveChanges(); foreach (var item in editbook.Img) { if (item != null) { Image im = new Image(); im.Book_Id = editbook.BookId; im.Img_URL = item; ctx.Images.Add(im); ctx.SaveChanges(); } } } else { return(NotFound()); } } return(Ok(editbook)); }