Add many to many relation without fetching child entity from database

你说的曾经没有我的故事 提交于 2019-12-11 03:42:15

问题


I have Book and Author entities with many to many relation and I am trying to save the relation (a record in junction table) with Database first approach.

I would like to start with my working version of 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 above code, I am fetching Author object (record) from database to add relation with book object. Whereas,I am not fetching Book object from database but attaching it to the context. Can't I do something similar with Author objects?

I have tried to do this with this code but it first adds new records to Author table and adds relation with book 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");

        }

回答1:


[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:


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

Also, have you tried adding the 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);
}


来源:https://stackoverflow.com/questions/11355019/add-many-to-many-relation-without-fetching-child-entity-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!