Add many of the many relationships without retrieving the child from the database

I have Book and Author objects with many relationships, and I'm trying to save the relationship (entry in the connection table) using the first approach to the database.

I would like to start with my working version of the code

[HttpPost] public ActionResult Edit(BookViewModel bookv) { Mapper.CreateMap<AuthorViewModel, Author>(); List<Author> authors = Mapper.Map<List<AuthorViewModel>, List<Author>> (bookv.Authors.ToList()); //remove authors from book object to avoid multiple entities with same key error bookv.Authors.Clear(); Mapper.CreateMap< BookViewModel,Book>(); Book book = Mapper.Map<BookViewModel,Book>(bookv); db.Books.Attach(book); book.Authors.Clear(); foreach (Author a in authors) { //Fetch Author and add relation to book object book.Authors.Add(db.Authors.Single(at=>at.AuthorId==a.AuthorId)); } db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } 

In the above code, I retrieve the Author object (record) from the database to add a relation to the book object. While I do not take the Book object from the database, but bind it to the context. Can't I do something similar with Author objects?

I tried to do this with this code, but first adds new entries to the Author table and adds relevance to books and newly created (unwanted) authors:

  [HttpPost] public ActionResult Edit(BookViewModel bookv) { Mapper.CreateMap<AuthorViewModel, Author>(); List<Author> authors = Mapper.Map<List<AuthorViewModel>, List<Author>>(bookv.Authors.ToList()); // bookv.Authors.Clear(); Mapper.CreateMap< BookViewModel,Book>(); Book book = Mapper.Map<BookViewModel,Book>(bookv); foreach (Author a in book.Authors) { db.Authors.Attach(a); } db.Books.Attach(book); foreach (Author a in authors) { book.Authors.Add(a); } db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } 
+2
asp.net-mvc asp.net-mvc-3 entity-framework many-to-many entity-framework-4
Jul 06 2018-12-12T00:
source share
2 answers
 [HttpPost] public ActionResult Edit(BookViewModel bookv) { //create maps Mapper.CreateMap<AuthorViewModel, Author>(); Mapper.CreateMap<BookViewModel, Book>(); //convert view objects to model objects Book book = Mapper.Map<BookViewModel, Book>(bookv); List<Author> authors = Mapper.Map<List<AuthorViewModel>, List<Author>> (bookv.Authors.ToList()); book.Authors.Clear(); db.Books.Attach(book); foreach (Author a in authors) { db.Authors.Attach(a); } book.Authors.Clear(); foreach (Author a in authors) { book.Authors.Add(a); } db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } 
+2
Aug 30 2018-12-12T00:
source share

Have you tried calling db.Books.Attach(book) before the db.Authors.Attach(a) loop?

Also, were you trying to add a book to the authors? Like a.Books.Add(book); instead?

IOW

 db.Books.Attach(book); foreach (Author a in book.Authors) { db.Authors.Attach(a); a.Books.Add(book); } 
+1
Jul 06 2018-12-12T00:
source share



All Articles