我用
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
之类的内容吗?
答案 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;