实体框架使用参考

时间:2011-05-29 14:20:14

标签: entity-framework

我用

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

但我让PetitionsContainer.Questions中的实体参与QuestionAuthor关系。

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

现在,ID为Author的{​​{1}}已在数据库中。

确实,每个问题必须有1位作者。

但是,我不能使用authorId代替AuthorsReference之类的内容吗?

1 个答案:

答案 0 :(得分: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;