简体   繁体   English

实体框架-收集相关数据

[英]Entity Framework - Load collection of related data

I'm using Entity Framework. 我正在使用实体框架。 I am trying to load relational data into an enumerable-property of an entity. 我正在尝试将关系数据加载到实体的可枚举属性中。 The entities are not related in the database so Entity Framework doesn't know how to join them. 实体在数据库中不相关,因此实体框架不知道如何联接它们。 Now I tried to write an extension method (for IQueryable) that joins two tables and loads and assigns the relational data to a specified property. 现在,我尝试编写一个扩展方法(用于IQueryable),该方法将两个表连接起来并加载并将关系数据分配给指定的属性。 Say we have an entity called Student: 假设我们有一个名为Student的实体:

public partial class Student
{
    public List<Book> Books { get; set; }
}

And an entity called Book 还有一个叫Book的实体

public partial class Book
{
   public int StudentId { get; set; }
}

Now I would like to load a Student with his assigned Books somehow like this: 现在,我想以某种方式向学生加载他分配的书:

[Pseudo]
context.Students.Include([setToJoin], [foreignKey], [key], [sourceCollection]) ;
=
context.Students.Include(context.Books, book => book.StudentId, student => student.Id, student.Books);

I really appreciate any help! 我非常感谢您的帮助!

You can manually create an association in EF. 您可以在EF中手动创建关联。 Please check here for the steps on how to accomplish that. 在此处查看有关如何完成此操作的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM