我正在使用Entity Framework 4.1开发数据库第一种方法。我有2个多对应的实体书和图书馆。我还有一个包含10列的联结表,用于存储有关图书库中特定副本的信息,其中包含与BOOK和LIBRARY实体的一对多关系。我创建了一个视图,该视图提交了一个带有子对象的JSON对象,该对象与我的BookVieMOdel的定义相匹配。该视图工作得很好但我在向控制器中的联结表添加新条目时遇到问题。我收到错误消息“ 不允许与处于”已删除“状态的实体添加关系 ”。
我猜这个错误是指被删除的对象,它被book.LibraryBookCopies.Clear();
删除但却无法想到以其他方式进行。
谢谢,
控制器:
[HttpPost]
public ActionResult Edit(BookViewModel bookv)
{
Mapper.CreateMap< BookViewModel,Book>();
Book book = Mapper.Map<BookViewModel,Book>(bookv);
db.Books.Attach(book);
db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified);
//Add library ref to each libraryBookCopies object in Book
foreach (LibraryBookCopy lBC in book.LibraryBookCopies)
{
lBC.Library = db.Libraries.Single(l => l.LibraryId == lBC.LibraryId);
}
//Remove all currently related records
book.LibraryBookCopies.Clear();
//Add copies from ViewModel to book object
foreach (LibraryBookCopy lBC in bookv.LibraryBookCopies)
{ book.LibraryBookCopies.Add(lBC); //Error here }
db.SaveChanges();
return RedirectToAction("Index");
}
答案 0 :(得分:2)
您已经说错误是&#34;不允许处于已删除状态的实体&#34;我想知道WillCascadeOnDelete是否存在问题并且正在删除本书。请参阅http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx
答案 1 :(得分:0)
也许在添加新记录之前尝试保存更改?
//Remove all currently related records
book.LibraryBookCopies.Clear();
db.SaveChanges();
//Add copies from ViewModel to book object
foreach (LibraryBookCopy lBC in bookv.LibraryBookCopies)
{ book.LibraryBookCopies.Add(lBC); //Error here }
答案 2 :(得分:0)
[HttpPost]
public ActionResult Edit(BookViewModel bookv)
{
//create maps
Mapper.CreateMap<LibraryBookCopyViewModel, LibraryBookCopy>();
Mapper.CreateMap<BookViewModel, Book>();
//convert view objects to model objects
Book book = Mapper.Map<BookViewModel, Book>(bookv);
List<LibraryBookCopy> bookCopiesFromView = Mapper.Map<List<LibraryBookCopyViewModel>, List<LibraryBookCopy>>(bookv.LibraryBookCopies);
List<LibraryBookCopy> toBeDeletedLibraryBookCopies = new List<LibraryBookCopy>();
//this has to be executed before db.Books.Attach
book.LibraryBookCopies.Clear();
//Add or update book copies
foreach (LibraryBookCopy bc in bookCopiesFromView)
{
if (bc.LibraryBookCopyId != 0)
{
db.LibraryBookCopies.Attach(bc);
db.ObjectStateManager.ChangeObjectState(bc, EntityState.Modified);
}
else
{
db.LibraryBookCopies.Attach(bc);
db.ObjectStateManager.ChangeObjectState(bc, EntityState.Added);
}
}
db.Books.Attach(book);
if (bookCopiesFromView.Count == 0) //if all the copies are delted from view, delete all from
{
db.LibraryBookCopies.Where(c => c.BookId == book.BookId).ToList().ForEach(l => db.LibraryBookCopies.DeleteObject(l));
}
else
{
toBeDeletedLibraryBookCopies = book.LibraryBookCopies.Except(bookCopiesFromView).ToList();
foreach (LibraryBookCopy bc in toBeDeletedLibraryBookCopies)
{
db.ObjectStateManager.ChangeObjectState(bc, EntityState.Deleted);
}
}
db.ObjectStateManager.ChangeObjectState(book, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}