嵌套属性的SetModifiedProperty

时间:2012-02-20 19:17:34

标签: entity-framework entity-framework-4.1 poco

如何为嵌套属性设置SetModifiedProperty?换句话说,我有一个实体作者,其实体有书籍集合,我只需更改书名。

    var existingAuthor = authors.FirstOrDefault(x => x.Id.Equals(authorId));
    var bestSellerBook = existingAuthor.Books.FirstOrDefault(x=> x.Id.Equals(bookId));
    existingAuthor.Name = "xxxxxxx";
    bestSellerBook.Name = "xxxxxxxxxxx";
    context.Authors.Attach(existingAuthor);  
    context.ObjectStateManager.GetObjectStateEntry(existingAuthor).SetModifiedProperty("Name");
    context.SaveChanges();

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您必须获取持有已更改的属性的实体的对象状态条目=在您的情况下它必须是书:

var existingAuthor = authors.FirstOrDefault(x => x.Id.Equals(authorId));
var bestSellerBook = existingAuthor.Books.FirstOrDefault(x=> x.Id.Equals(bookId));
bestSellerBook.Name = "xxxxxxxxxxx";
context.Authors.Attach(existingAuthor);  
context.ObjectStateManager.GetObjectStateEntry(bestSellerBook).SetModifiedProperty("Name");
context.SaveChanges();