Anil C
Anil C

Reputation: 1065

EDMX Entities not populating data

I am working on a project where I am using Entity Framework.

There is a very strange problem I came across.
I am getting the User table information via EDMX which has relation with a Contacts table (through UserID column).

Now, User collection has retrieved ContactID and it should also retrieve Contact information for all the Users whereas the Contact entity is populated for some of the Users and for others it is NULL. But, if I check the value in ContactID it is available but Contact entity is NULL for some Users. I never faced this kind of issue. Any suggestions shall be highly apppreciated......

Upvotes: 1

Views: 52

Answers (1)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104811

First of all, make sure the keys and associations are properly set up. If you suspect that's the case, read this article thoroughly.


Then choose from one of the following options:

Include references in your query:

var query = context.Contacts
              .Include(c => c.User)
              .SingleOrDefault(c => c.UserID == userID);

Enable Lazy Loading
Another option is explicitly enabling lazy-loading in the context's constructor (or by overriding OnModelCreating):

public MyContext()
{
    Configuration.LazyLoadingEnabled = true;
}

Upvotes: 1

Related Questions