2011-05-29 59 views
0

我用實體框架使用的參考

p.AuthorsReference.EntityKey = new System.Data.EntityKey("PetitionsContainer.Authors", "Id", authorId); 

但我得到的PetitionsContainer.Questions實體參加QuestionAuthor關係。現在

0 related 'Author' were found. 1 'Author' is expected.

,該Author與標識authorId已經在數據庫中。

確實每個問題都必須有1位作者。

雖然,我不能使用AuthorsReference而不是p.Authors.Add(new Author())之類的東西嗎?

+0

如果您發佈的代碼,XML或數據樣本,** **請在高亮文本編輯器這些線路上,並點擊了「代碼示例」按鈕('{}')編輯器工具欄可以很好地格式化和語法突出顯示它! – 2011-05-29 14:22:19

回答

0

如果您設置了參考,您還必須填寫作者。您可以嘗試使用此:

// Attach a dummy author to the context so that context believes that you 
// loaded the author from the database 
Author a = new Author { Id = authorId }; 
context.Authors.Attach(a); 
// Now assign existing author to the question 
question.Author = a;