Entity framework 实体框架引用的使用

Entity framework 实体框架引用的使用,entity-framework,Entity Framework,我用 但是我在请愿书Container中得到了实体。问题参与了问题作者关系 找到了0个相关的“作者”。应为1名“作者”。 现在,Id为authorId的Author已在数据库中 的确,每个问题必须有一位作者 但是,我不能使用AuthorsReference而不是类似于p.Authors.Add(new Author())?的东西吗?如果设置了引用,还必须填写作者。您可以尝试使用以下方法: p.AuthorsReference.EntityKey = new System.Data.EntityK

我用

但是我在请愿书Container中得到了实体。问题参与了
问题作者
关系

找到了0个相关的“作者”。应为1名“作者”。

现在,Id为
authorId
Author
已在数据库中

的确,每个问题必须有一位作者


但是,我不能使用
AuthorsReference
而不是类似于
p.Authors.Add(new Author())

的东西吗?如果设置了引用,还必须填写作者。您可以尝试使用以下方法:

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

如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以很好地格式化和语法突出显示它!
// 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;