简体   繁体   中英

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. Say we have an entity called Student:

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

And an entity called 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. Please check here for the steps on how to accomplish that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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