C# Entity Framework with linq returns null reference
c#, entity, frameworks, linq
Solution
try to eagerly load `UserRole` (join):
context.User.Include("UserRole").Where(i => i.id == id).FirstOrDefault();
or enable lazy loading first:
context.ContextOptions.LazyLoadingEnabled = true;
context.User.Where(i => i.id == id).FirstOrDefault();
otherwise there is no relation to a `UserRole` in your database...
Problem
I have a problem with entity framework in C#. I have 2 entities, User and UserRole. They are bond by relationships User *->1 UserRole Whenever I use this query in a function: ``` User user = context.User.Where(i => i.id == id).FirstOrDefault(); return user.UserRole.accessLevel; ``` The query returns user, but UserRole is null. The User table has roleId which is related to id of UserRole, and the value of roleId when debugging is correct, although UserRole entity is null. This is strange as it never happened before... I already made sure that my relationships in model and database are correct. I have correct rows added to database. EDIT: Sorry, I should've mentioned I use custom testable database controller: ``` public class DBController : IUnitOfWork { readonly ObjectContext context; const string ConnectionStringName = "MarketPlaceDBEntities"; public DBController() { var connectionString = ConfigurationManager .ConnectionStrings[ConnectionStringName] .ConnectionString; context = new ObjectContext(connectionString); } public void Commit() { context.SaveChanges(); } public IObjectSet<Category> Category { get { return context.CreateObjectSet<Category>(); } } public IObjectSet<ItemComment> ItemComment { get { return context.CreateObjectSet<ItemComment>(); } } public IObjectSet<ItemRating> ItemRating { get { return context.CreateObjectSet<ItemRating>(); } } public IObjectSet<Item> Item { get { return context.CreateObjectSet<Item>(); } } public IObjectSet<ItemSale> ItemSale { get { return context.CreateObjectSet<ItemSale>(); } } public IObjectSet<ItemScreenshot> ItemScreenshot { get { return context.CreateObjectSet<ItemScreenshot>(); } } public IObjectSet<UserRole> UserRole { get { return context.CreateObjectSet<UserRole>(); } } public IObjectSet<User> User { get { return context.CreateObjectSet<User>(); } } } ``` And I do operations via it. Maybe it has to do something with my prob. ``` interface IUnitOfWork { IObjectSet<Category> Category { get; } IObjectSet<ItemComment> ItemComment { get; } IObjectSet<ItemRating> ItemRating { get; } IObjectSet<Item> Item { get; } IObjectSet<ItemSale> ItemSale { get; } IObjectSet<ItemScreenshot> ItemScreenshot { get; } IObjectSet<UserRole> UserRole { get; } IObjectSet<User> User { get; } void Commit(); } ``` I had this whole thing working before, but don't know why it went wrong.. EDIT2: Solved! Thanks RicoSuter. Enabling lazy loading in constructor of my db controller solved the problem. I thought it was already enabled, because it was set to true in database model, but it looks like that when creating a new context, you have to enable it manually again. ``` public DBController() { var connectionString = ConfigurationManager .ConnectionStrings[ConnectionStringName] .ConnectionString; context = new ObjectContext(connectionString); context.ContextOptions.LazyLoadingEnabled = true; } ```