35

I'm pretty new in the ASP .NET MVC world. Maybe, that's the reason I can't explain to myself the cause of what is, for me, an annoying problem.

I have one class with One-To-Many relashionship.

class MyClass{
    public List<OtherClass> otherClasses {get;set;}
}

When I'm persisting one instance of this class, I fill it's relationship with an empty List<>

MyClass myClass = new MyClass(){ otherClasses = new List<OtherClass>() }
context.myClass.Add(myClass);

The problem is that, when I try to retrieve that instance, and for any reason, I try to access that list, system gives me a Null Reference Exception...

My question is: why doesn't EF return empty lists instead of null ones? Especially in this case, that I'm persisting it with an empty list?

There's any way to avoid verifing if instances are null?

1
  • what is you entity framework class? Commented Feb 12, 2012 at 5:57

3 Answers 3

31

You should have your entity create those lists in the constructor. EF doesn't create dependent collections, and expects the entity to do so.

So, your case, you would make your entity like this:

class MyClass{ 
    public List<OtherClass> _otherClasses {get;set;} 

    public MyClass() {
        _otherClasses = new List<OtherClass>();
    }
} 
Sign up to request clarification or add additional context in comments.

8 Comments

This definitely solves my problem! Thanks for your help! So the EF calls object constructor before loading it with the persisted info, don't it?
No, EF doesn't "call" the constructor. C# does. Whenever an object is created, the constructor is called. Even when EF materializes objects from the db.
@MystereMan you're totally right, but, the point is that sometimes this constructor call is somehow bypassed! For instance, in WCF binary deserialization
@Alireza - If the constructor is not called in WCF, it's because WCF is likely not newing the object, but using another mechanism to materialize it. Serialization is a complex process and they often do things at a low level to accomplish it.
@ErikFunkenbusch I can't find any EF related documentation telling "EF expects the entity to creates dependant collections".
|
17

Make the otherClasses collection virtual. This will enable EF to lazy load the collection.

class MyClass{
    public virtual List<OtherClass> otherClasses {get;set;}
}

Otherwise use eager loading with Include method.

context.myClass.Include(m => m.otherClasses).SingleOrDefault(m => m.Id == foo);

2 Comments

Indeed, lazy loading resolves my problem... But I'm trying to avoid lazy loading because it was giving me Circular Reference Serialization Error when I was using JSON requests. Anyway, I'm glad for your help. Thanks!
For circular reference problems, you can use attributes like JsonIgnore to prevent a back-linking navigation property from being serialized.
0

So, if I understand correctly, you are adding an empty List<OtherClass> to the context and then trying to retrieve it.

I guess you have to think about how the context will track and query entities that are in its context. This is usually done by the Key of the entity. In your example, you have not given the entity a Key, therefore, the context has no handle on the entity.

Therefore, when you query, the context doesn't find an object and returns null.

If you want to initialize a new entity, I would recommend to give it at least a Key (usually the Id property), and then select by that key when you lookup later.

Hope this helps.

1 Comment

In fact, my real classes have all an ID attribute. It was only an example where I forgot to include the ID :P. Anyway, thanks for your answer!

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.