Click here to Skip to main content
15,908,675 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Thought of the day Pin
User 1106097923-Jun-16 6:20
User 1106097923-Jun-16 6:20 
GeneralRe: Thought of the day Pin
den2k8823-Jun-16 5:44
professionalden2k8823-Jun-16 5:44 
GeneralVS has broken the Google Pin
Marco Bertschi23-Jun-16 4:12
protectorMarco Bertschi23-Jun-16 4:12 
GeneralRe: VS has broken the Google Pin
Ravi Bhavnani23-Jun-16 4:37
professionalRavi Bhavnani23-Jun-16 4:37 
GeneralRe: VS has broken the Google Pin
Marco Bertschi23-Jun-16 21:37
protectorMarco Bertschi23-Jun-16 21:37 
GeneralRe: VS has broken the Google Pin
KarstenK23-Jun-16 5:11
mveKarstenK23-Jun-16 5:11 
QuestionRe: VS has broken the Google Pin
Philippe Mori23-Jun-16 6:04
Philippe Mori23-Jun-16 6:04 
GeneralRe: VS has broken the Google Pin
Richard Deeming23-Jun-16 7:31
mveRichard Deeming23-Jun-16 7:31 
OK, time for a random guess of what your code looks like:
C#
public Foo GetFoo(int id)
{
    using (var context = new YourDbContext())
    {
        return context.Foos.FirstOrDefault(foo => foo.Id == id);
    }
}
...
Foo foo = GetFoo(42);
Bar bar = foo.Bar;

You've retrieved an entity instance without loading any of its related entities. You've then disposed of the DbContext before trying to load a related entity. The lazy-loading proxy that EF created for your entity is then trying to load the related entity from the database, using the previously-disposed DbContext.

You can either turn off lazy loading, in which case the navigation property will return null; or you can explicitly load the related entities before you dispose of the DbContext; or you can use the Include extension method to eagerly load the related entities at the same time as the main entity.
Entity Framework Loading Related Entities[^]

And now, you've managed to sneak a programming question into the Lounge! Laugh | :laugh:



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralHello I'm new Pin
Member 1260029623-Jun-16 3:50
Member 1260029623-Jun-16 3:50 
GeneralRe: Hello I'm new Pin
908236523-Jun-16 3:51
908236523-Jun-16 3:51 
GeneralRe: Hello I'm new Pin
Member 1260029623-Jun-16 4:44
Member 1260029623-Jun-16 4:44 
GeneralRe: Hello I'm new Pin
OriginalGriff23-Jun-16 4:48
mveOriginalGriff23-Jun-16 4:48 
GeneralRe: Hello I'm new Pin
devenv.exe23-Jun-16 20:06
professionaldevenv.exe23-Jun-16 20:06 
GeneralRe: Hello I'm new Pin
Pete O'Hanlon23-Jun-16 3:53
mvePete O'Hanlon23-Jun-16 3:53 
GeneralRe: Hello I'm new Pin
Eddy Vluggen23-Jun-16 3:53
professionalEddy Vluggen23-Jun-16 3:53 
GeneralRe: Hello I'm new Pin
Marc Clifton23-Jun-16 4:06
mvaMarc Clifton23-Jun-16 4:06 
GeneralRe: Hello I'm new Pin
Maximilien23-Jun-16 4:09
Maximilien23-Jun-16 4:09 
GeneralRe: Hello I'm new Pin
jeron123-Jun-16 4:25
jeron123-Jun-16 4:25 
GeneralRe: Hello I'm new Pin
Ravi Bhavnani23-Jun-16 4:39
professionalRavi Bhavnani23-Jun-16 4:39 
GeneralRe: Hello I'm new Pin
Member 1260029623-Jun-16 4:44
Member 1260029623-Jun-16 4:44 
GeneralRe: Hello I'm new Pin
OriginalGriff23-Jun-16 4:49
mveOriginalGriff23-Jun-16 4:49 
GeneralRe: Hello I'm new Pin
Power Puff Boy23-Jun-16 4:42
Power Puff Boy23-Jun-16 4:42 
JokeRe: Hello I'm new Pin
ZurdoDev23-Jun-16 5:10
professionalZurdoDev23-Jun-16 5:10 
GeneralRe: Hello I'm new Pin
Member 1260029623-Jun-16 5:21
Member 1260029623-Jun-16 5:21 
GeneralRe: Hello I'm new Pin
den2k8823-Jun-16 4:42
professionalden2k8823-Jun-16 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.