问题描述
我有Book和Author实体多对多的关系,我想保存数据库的第一种方法的关系(在路口表中的记录)。
我想先从code的工作我的版本
[HttpPost]
公众的ActionResult编辑(BookViewModel bookv)
{
Mapper.CreateMap< AuthorViewModel,作者及GT;();
清单<作者>作者= Mapper.Map<名单,LT; AuthorViewModel>中列出<作者>>
(bookv.Authors.ToList());
//从书中删除对象的作者,以避免与同一键错误多个实体
bookv.Authors.Clear();
Mapper.CreateMap< BookViewModel,图书>();
书书= Mapper.Map< BookViewModel,图书>(bookv); db.Books.Attach(书);
book.Authors.Clear();
的foreach(在作者作者一)
{
//获取作者,并添加相对于预定目标
book.Authors.Add(db.Authors.Single(AT = GT; at.AuthorId == a.AuthorId));
} db.ObjectStateManager.ChangeObjectState(书,EntityState.Modified);
db.SaveChanges();
返回RedirectToAction(「指数」); }
在上面code,我从数据库中获取Author对象(记录)添加与书对象关系。然而,我不是从数据库中获取图书的对象,但其附加到上下文。我不能做Author对象相似的地方?
我曾尝试与该code要做到这一点,但它首先增加了新的记录,作者表格,并将与有关的书和新创建的(不必要的)作者:
[HttpPost]
公众的ActionResult编辑(BookViewModel bookv)
{
Mapper.CreateMap< AuthorViewModel,作者及GT;();
清单<作者>作者= Mapper.Map<名单,LT; AuthorViewModel>中列出<作者>>(bookv.Authors.ToList());
// bookv.Authors.Clear();
Mapper.CreateMap< BookViewModel,图书>();
书书= Mapper.Map< BookViewModel,图书>(bookv); 的foreach(在book.Authors作者一)
{
db.Authors.Attach(一);
} db.Books.Attach(书); 的foreach(在作者作者一)
{
book.Authors.Add(一);
}
db.ObjectStateManager.ChangeObjectState(书,EntityState.Modified);
db.SaveChanges();
返回RedirectToAction(「指数」); }
[HttpPost]
公众的ActionResult编辑(BookViewModel bookv)
{
//创建地图
Mapper.CreateMap< AuthorViewModel,作者及GT;();
Mapper.CreateMap< BookViewModel,图书>(); //转换视图对象模型对象
书书= Mapper.Map< BookViewModel,图书>(bookv);
清单<作者>作者= Mapper.Map<名单,LT; AuthorViewModel>中列出<作者>> (bookv.Authors.ToList()); book.Authors.Clear();
db.Books.Attach(书);
的foreach(在作者作者一个){db.Authors.Attach(一); } book.Authors.Clear(); 的foreach(在作者作者一个){book.Authors.Add(一); } db.ObjectStateManager.ChangeObjectState(书,EntityState.Modified);
db.SaveChanges();
返回RedirectToAction(「指数」); }
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");
}
[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");
}
这篇关于添加多对多的关系,而不从数据库中获取子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!