如何解决实体框架的使用引用
| 我用p.AuthorsReference.EntityKey = new System.Data.EntityKey(\"PetitionsContainer.Authors\",\"Id\",authorId);
但我让PetitionsContainer.Questions
中的实体参与QuestionAuthor
关系。
0 related \'Author\' were found. 1 \'Author\' is expected.
现在,带有IDauthorId
的Author
已在数据库中。
的确,每个问题必须有1位作者。
但是,我不能用AuthorsReference
代替p.Authors.Add(new Author())
吗?
解决方法
如果您设置参考文献,则还必须填写作者。您可以尝试使用以下方法:
// 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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。