Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I've done all the readings I can find on Entity and lazy loading, as well as other questions here, but for the life of me I can't get this working. Here's my SQL for the DB:

CREATE TABLE corporations(
corporationID bigint PRIMARY KEY
);

CREATE TABLE Character(
personID bigint PRIMARY KEY,
corporationID int NOT NULL,
FOREIGN KEY (corporationID) REFERENCES corporations(corporationID)
);

And the Entity code to get it (*EDITED from original, still broken*):

DBEntities context = new DBEntities();
public Character Character_GetByID(long CharacterID)
        {
            context.ContextOptions.LazyLoadingEnabled = true;
            Character character = context.Characters.Where(c => c.CharacterID == CharacterID).FirstOrDefault();      
            return character;
        }

So from my understanding, with this I should be able to go

Character char = Character_GetByID(characterID);
Corporation corp = char.Corporation;

The "char.Corporation" object exists, Entity created it properly from the foreign key. But when I run the above code, "corp" always returns as NULL (even though I know for sure that the relevant corporation is in the DB).

One thing I did notice is that in the auto-generated Entity Character object, it has the function:

public virtual Corporation Corporation
        {
            get { return _corporation; }
            set
            {
                if (!ReferenceEquals(_corporation, value))
                {
                    var previousValue = _corporation;
                    _corporation = value;
                    FixupCorporation(previousValue);
                }
            }
        }

Which seems odd, because I would assume that with lazy loading the "get" function would be something like "if null, try to get Corporation from database". Any thoughts would be highly appreciated.

*EDIT* Request for how lazy loading is configured:

In my Context class, for every constructor I have

this.ContextOptions.LazyLoadingEnabled = true;

And as you can see from the first C# function up above, I tried setting it as true in the function itself, just before querying it.

share|improve this question
    
Could you paste how do you've configured the lazy loading? – ivowiblo May 10 '12 at 19:59
    
use a .Include(x=> x.Corporation) ; – Greens May 10 '12 at 20:03
    
@ivowiblo: I added an edit to show what I've done with the LazyLoading config, but if there's configuration somewhere else I suppose I don't know about it – Jordan May 10 '12 at 20:06
    
@Greens: yes, I tried that and it worked, but this is a poor workaround because I don't want it to ALWAYS have this data loaded (the reason being that this is just one example of many Entity objects, none of which are working, and some have very large datasets that I don't want to always load because they're rarely used) – Jordan May 10 '12 at 20:07
    
Is your DbContext still alive (not disposed) when you try to reach the data by lazy loading? – JOBG May 10 '12 at 20:11
up vote 3 down vote accepted

Lazy loading is a functional provided by object context (DBEntities class instance in your case). So, it will work only when entity object is attached to some DBEntities instance. When Character_GetByID method is done, context is disposed, entity is detached, and your lazy loading request cannot be performed.

share|improve this answer
    
So I went ahead and got rid of the "using (var context = new DBEntities())" and replaced it by setting a private variable in the whole class (so it's not disposed when the method exits). Unfortunately it's still not working though... – Jordan May 10 '12 at 20:27
    
All fixed! Context was still being disposed somehow, got it working though. Thanks for the help! – Jordan May 10 '12 at 20:37
    
But you're still disposing your context somewhere, right? Because you're going to have a bad time if not. – Mike Cole Feb 4 '14 at 17:40

Remove the using statement, as when you use this dbcontext is disposed right away

using (var context = new DBEntities())
{
   ...
}//context is disposed here... lazy loading is not possible
share|improve this answer

use a .Include(x=> x.Corporation) ;

For some info on loading related data.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

share|improve this answer
    
yes, I tried that and it worked, but this is a poor workaround because I don't want it to ALWAYS have this data loaded (the reason being that this is just one example of many Entity objects, none of which are working, and some have very large datasets that I don't want to always load because they're rarely used) – Jordan May 10 '12 at 20:11
    
@Greens, it's the eager loading, not lazy. – Dennis May 10 '12 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.