locked
Returning data from the parent table returning data from Foreign Key tables aswell RRS feed

  • Question

  • In ASP.NET Web API Service model is made of Entity classes that are generated by ADO.NET Entity Framework Data Model database first approach.

    When the Model is returned to the client in Service response, data from the Foreign key tables is also returned. For example: Customer table contains Contact table as the Foreign key table.

    The Entity Framework generating the POCO (Entity) as below.

    public partial class Customer
    {
    public int Id {get;set;}
    public FirstName {get;set}
    public LastName {get;set;}
    
    //Foreign key table
    public virtual Contact Contact {get;set;}
    }

    The Service model is:

    public class Customer
    {
            public Customer customer {get;set;}
    }

    From the API Controller an instance of the model class is created, data is retrieved from the database and returned to the client.

    The issue is "when all the records or single record from the Customer table is returned to the client, it is returning the data from Foreign key(Referenced) tables as well.  Say there are about 100 contacts in Contact table, when a request for one resource is made from the web service client, the data returned is including all the data from the Customer table as well.

    Help appreciated to address this behavior.

    Regards


    Apriori algorithm [association rule]


    • Edited by Raju Golla Thursday, April 24, 2014 4:42 PM
    Thursday, April 24, 2014 4:40 PM

Answers

  • In the Entity Framework runtime, the default value of the "LazyLoadingEnabled" property in an instance of ObjectContext is false. As the data model has been generated by the ADO.NET Entity Data model Wizard (Tool) the generated code set the LazyLoadingEnabled to true

    causing all the data from navigation or related objects (Foreign Key Tables - as virtual properties in parent Entity class) to be returned.

    To resolve this issue the simple and straight forward solution is set the "LazyLoaddingEnabled" property for the database context "False" in the Generic Repository class.

    More at http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx

    http://msdn.microsoft.com/en-ie/data/jj574232.aspx


    Apriori algorithm [association rule]

    • Marked as answer by Raju Golla Friday, April 25, 2014 11:05 AM
    Friday, April 25, 2014 11:05 AM

All replies

  • Well, that's why I use the Database First approach and use DTO(s). This gives me the ability to bring back only what I need and leave any children to a parent behind. Sometimes, you have to hand roll your own object and bring back only what you need.
    Thursday, April 24, 2014 9:19 PM
  • Thanks for your help Damold.

    But your response does not in resolving the issue as I am using Database first approach.

    Further help appreciated.


    Apriori algorithm [association rule]

    Friday, April 25, 2014 8:41 AM
  • In the Entity Framework runtime, the default value of the "LazyLoadingEnabled" property in an instance of ObjectContext is false. As the data model has been generated by the ADO.NET Entity Data model Wizard (Tool) the generated code set the LazyLoadingEnabled to true

    causing all the data from navigation or related objects (Foreign Key Tables - as virtual properties in parent Entity class) to be returned.

    To resolve this issue the simple and straight forward solution is set the "LazyLoaddingEnabled" property for the database context "False" in the Generic Repository class.

    More at http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx

    http://msdn.microsoft.com/en-ie/data/jj574232.aspx


    Apriori algorithm [association rule]

    • Marked as answer by Raju Golla Friday, April 25, 2014 11:05 AM
    Friday, April 25, 2014 11:05 AM