locked
What is include function does in EF RRS feed

  • Question

  • User264732274 posted

    just see the code

    var albums = this.db.Albums.Include(a => a.Genre).Include(a => a.Artist)
             .OrderBy(a => a.Price);

    what the above code is doing? what is include function does in EF ?

    Thursday, October 15, 2015 8:47 AM

Answers

  • User281315223 posted

    The Include() method actually is used to pull related tables and populate them within your entities. So in the case of your query, it will return a collection of Album entities with the Genre and Artist properties already populated (without the includes, these would be null as they are in separate tables).

    It basically identifies foreign key relationships on your actual entity and pulls the related entity objects as well.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 8:52 AM
  • User603616845 posted

    Hi,

    Note that Include is an extension method in the System.Data.Entity namespace so make sure you are using that namespace. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. 

    Include in EF as like JOIN in SQL. It is used to join multiple model [table] in the query.

    Hope this will help.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 8:57 AM
  • User859425685 posted

    Hi Sudip

    You will have to include all 3 tables in your EDMX. Also there must be proper Primary- Foreign Key relation defined in the database otherwise the code will not compile as the Albums entity will not have Genre and Artist properties.

    Hope this helps.

    Regards

    Tajinder Sarao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 12:07 PM
  • User-2007877324 posted

    i want to create relationship with EDMX designer

    Entity Framework Designer provides a graphical interface to define the relationships

    Relationships/Associations with the EF Designer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2015 12:34 AM
  • User-2007877324 posted

    If you already had the POCO classes running, you just need to move them to a different folder and change the namespace.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2015 5:55 AM

All replies

  • User281315223 posted

    The Include() method actually is used to pull related tables and populate them within your entities. So in the case of your query, it will return a collection of Album entities with the Genre and Artist properties already populated (without the includes, these would be null as they are in separate tables).

    It basically identifies foreign key relationships on your actual entity and pulls the related entity objects as well.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 8:52 AM
  • User603616845 posted

    Hi,

    Note that Include is an extension method in the System.Data.Entity namespace so make sure you are using that namespace. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. 

    Include in EF as like JOIN in SQL. It is used to join multiple model [table] in the query.

    Hope this will help.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 8:57 AM
  • User264732274 posted

    @Raion : i hardly use EF rather use MSDAAB.

    suppose in my db first when we create EDMX file then suppose i drag Album table only then when we execute this below code

    var albums = this.db.Albums.Include(a => a.Genre).Include(a => a.Artist)
             .OrderBy(a => a.Price);

    how it can fetch data from Genre and Artist table?

    if we drop 3 table in EDMX say Album ,Genre and Artist but if they have no relation then can we execute the above query ?

    just tell me what is the prerequisite to execute the above query if i am working with db first approach?

    thanks

    Thursday, October 15, 2015 9:33 AM
  • User264732274 posted

    @Mukesh if i am working with db first approach then what is the prerequisite to execute the below quuery

    var albums = this.db.Albums.Include(a => a.Genre).Include(a => a.Artist)
             .OrderBy(a => a.Price);

    do i need to drag all the 3 tables in EDMX designer?

    all 3 tables must have relation ship? if there is no relation among the 3 table then what would happen ?

    thanks

    Thursday, October 15, 2015 9:35 AM
  • User859425685 posted

    Hi Sudip

    You will have to include all 3 tables in your EDMX. Also there must be proper Primary- Foreign Key relation defined in the database otherwise the code will not compile as the Albums entity will not have Genre and Artist properties.

    Hope this helps.

    Regards

    Tajinder Sarao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 12:07 PM
  • User264732274 posted

    @tssarao suppose there is no relation between 3 tables among Albums entity will not have,Genre and Artist but can i create relation after dragging those table in EDMX designer. i want to create relationship with EDMX designer.......is it possible? thanks

    Thursday, October 15, 2015 2:06 PM
  • User859425685 posted

    @sudip_inn yes I believe you can do that. EDMX relations will ensure right SQL is generated to get the data from related tables.

    Regards

    Tajinder Sarao

    Thursday, October 15, 2015 2:36 PM
  • User-2007877324 posted

    i want to create relationship with EDMX designer

    Entity Framework Designer provides a graphical interface to define the relationships

    Relationships/Associations with the EF Designer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2015 12:34 AM
  • User264732274 posted

    just a different question. i believe u know and give me idea that how to separate POCO classes when working with EF db first approach. i want entity classes should be store in my model folder of asp.net mvc project. any idea. thanks

    Friday, October 16, 2015 5:32 AM
  • User-2007877324 posted

    If you already had the POCO classes running, you just need to move them to a different folder and change the namespace.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2015 5:55 AM