Tim
Tim

Reputation: 7421

Prevent webapi from returning all data from associated tables

I have a table called MemberCompany which has a record for each company a member has. the model is below. When i query it via a webapi method passing in the memberid, i can see in debug mode that it returns the one company for that member, however when i run it in the browser i can see it returns the entire list of members also. Is it possible to just return a collection of membercompany records without the two referenced tables? I commented out the initial code to include these two tables but they appear to still be being included in the response.

public partial class MemberCompany
    {
        public int id { get; set; }
        public int membership_id { get; set; }
        public string company_name { get; set; }
        public string company_address1 { get; set; }
        public string company_address2 { get; set; }
        public string company_town_city { get; set; }
        public Nullable<int> company_county { get; set; }
        public string company_postcode { get; set; }
        public string company_tel { get; set; }
        public string company_fax { get; set; }
        public string company_email { get; set; }
        public string company_contact { get; set; }
        public string company_web { get; set; }
        public string company_country { get; set; }
        public Nullable<System.DateTime> last_updated { get; set; }
        public Nullable<decimal> latitude { get; set; }
        public Nullable<decimal> longitude { get; set; }

        public virtual counties counties { get; set; }
        public virtual members members { get; set; }
    }

WebAPI

  [HttpGet("admin/api/membercompany/member/{member_id}")]
            public IEnumerable<MemberCompany> GetByMember(int member_id)
            {
                var Companies = db.MemberCompanies
                 //   .Include(t => t.counties)
                    //.Include(t => t.members)
                    .Where(m => m.membership_id == member_id);
                return Companies.AsEnumerable();
            }

Upvotes: 0

Views: 934

Answers (1)

Alex Paven
Alex Paven

Reputation: 5549

Turn off lazy loading for the context. My best guess is it's on and the entities are loaded when the graph is serialized...

Note: that's actually a good idea in a web app and I'd recommend you do it globally, so that you don't get bitten by performance issues due to lazy loading later, and always know precisely what you'll return.

Upvotes: 1

Related Questions