Entity Framework always includes data that is in context even if I don't ask for it

后端 未结 5 831
野趣味
野趣味 2021-02-07 02:18

I am using MVC.NET web api, EF with DB first, and I have lazy loading turned off on my context. EF is returning way too much data, even with LazyLoading turned off.

For

5条回答
  •  忘掉有多难
    2021-02-07 03:02

    The behaviour your are seeing is called Relationship Fixup and you cannot disable it.

    If you are loading users with roles to serialize them and sent them to somewhere I guess that you don't want to track changes of entities in the context they have been loaded in. So, there is no need to attach them to the context and you can use:

    return db.Users.Include(u => u.Role).AsNoTracking();
    

    Or use a projection into an object specialized for serialization, as suggested by @STLRick.

提交回复
热议问题