1

I am trying to get informations from my User table with User's ID but my controller returns all of navigation properties that connected with foreign keys to other tables.

Code is simply webapi method:

[ResponseType(typeof(User))]
    public IHttpActionResult GetUser(int id)
    {
        User user = db.User.Find(id);
        if (user == null)
        {
            return NotFound();
        }

        return Ok(user);
    }

It doesn't return only User, it returns all navigation properties connected with this User.

How can I return only User?

This post explains selected properties. I want all of properties, but controller returns me all of connected tables datas with foreign keys too.

8
  • Hmm, Find shouldn't load the navigation properties. Are you saying the FKs are populated or the navigation properties? stackoverflow.com/questions/5764391/… Commented Jan 12, 2016 at 18:34
  • @SteveGreene thanks, I've found solution. Commented Jan 12, 2016 at 18:37
  • Possible duplicate of Entity Framework - Load only Selected properties Commented Jan 12, 2016 at 18:40
  • 1
    Yes, I would go with Fabio's suggestion and use a projection. Exposing your entity model in an api is dangerous. Commented Jan 12, 2016 at 18:51
  • So what is the best practice if there are so much column at table? Commented Jan 12, 2016 at 18:58

1 Answer 1

2

The best practice for Web-Applications is to disable the LazyLoadin in your DbContext.

context.Configuration.LazyLoadingEnabled = false;
Sign up to request clarification or add additional context in comments.

3 Comments

so you are dissident with @FabioLuz, he says it works but its not solution for this.
Actually, it's always better to return specific view model containing just the needed data. But it always up to your project if it's huge and you want to add this kind of complexity or it's simple and you want just to go with your Entities.
@TuğrulEmreAtalay abi please accept my answer if you found it useful ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.