|
OK, time for a random guess of what your code looks like:
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!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I just joined this forum, so wanted to say hi! sorry if this is the wrong place for introductions, I'm new here so still figuring out how it all works!
|
|
|
|
|
Hello new!
I am not a number. I am a ... no, wait!
|
|
|
|
|
Haha .. well my name is Jade. I am looking to start a career in working for web hosting companies, and wanted to join this forum to do some research
I have an interview next week ...
|
|
|
|
|
Plenty to look at here!
Search[^] is a good place to start.
But...you'd probably be best off if you start by researching the company you are interviewing for, not the subject. Find out what they do, what they use, and how that relates to your experience and knowledge. Then use research to brush up your knowledge - but never to pretend you are experienced in stuff you don't know! That way may get you through the interview, but it won't let you keep the job once it becomes apparent you know little or nothing....
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
This is the right place. Sit back awhile and tell us about yourself.
This space for rent
|
|
|
|
|
Welcome New
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello, I'm old (but young at heart!)
Tell us about yourself! What inspired you to join?
Marc
|
|
|
|
|
Welcome New, and don't feed the trolls animals.
I'd rather be phishing!
|
|
|
|
|
New, is that a German name?
Welcome.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Welcome to CodeProject!
/ravi
|
|
|
|
|
Hmmm I wrote a reply but I don't think it got posted ...
Anyway, I'm Jade. I am hoping to start a career working for web hosting companies and wanted to join this forum to do some research.
I have an interview next week ... eeek
modified 27-Jun-16 9:34am.
|
|
|
|
|
It was intercepted by the automated spam system and required human intervention.
We don't like spam here, and the system sometimes makes mistakes - as in this case. I let it through.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi and welcome and I won't call you New.
Kitty at my foot and I waAAAant to touch it...
|
|
|
|
|
Power Puff Boy wrote: and I won't call you New. Well that's a new response.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
|
Hello new, it's good to be introduced to a serious operator!
Yes, I make terrible jokes.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
Where was your coat?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Very far - 34° C. I'd peel away my very skin.
GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver
When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani
|
|
|
|
|
Hello New welcome!
New version: WinHeist Version 2.2.2 Beta I told my psychiatrist that I was hearing voices in my head. He said you don't have a psychiatrist!
|
|
|
|
|
Welcome to the Forum new User(name); hope you enjoy your time here
Rules for the FOSW ![ ^]
if(this.signature != "")
{
MessageBox.Show("This is my signature: " + Environment.NewLine + signature);
}
else
{
MessageBox.Show("404-Signature not found");
}
|
|
|
|
|
Member 12600296 wrote: I'm new here so still figuring out how it all works! Welcome[^]
|
|
|
|
|
Welcome, hi.
Bruno
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Welcame Jade! You will find a lot of gems around here
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|