2011-05-29 87 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;