public class Page
{
//...
public virtual Page Parent { get; set; }
}
需要設置父字段爲空。嘗試這樣做,但沒有運氣:
// Existing entity
Page pageAttached = db.Pages.First(x => x.Id == page.Id);
db.Entry(pageAttached).CurrentValues.SetValues(page);
if (model.ParentId != null)
pageAttached.Parent = db.Pages.First(x => x.Id == model.ParentId);
else
pageAttached.Parent = null; //does nothing
db.SaveChanges();
我想你的意思是'Page pageAttached = db.Pages.Include(「Parent」)。First(x => x.Id == page.Id);'它工作。你能解釋嗎? – Wonder 2012-03-17 08:19:35