Burgese
Burgese

Reputation: 43

Navigation property is null in Entity Framework

I'm building a simple social network using ASP.NET MVC and ive ecnountered a problem. I'm very new to this so this might be an easy fix but here goes:

I'm trying to setup a system where a user (ApplicationUser) can send a friend request to another user and I've gotten so far that the friend requests are being registered in the database with the sender and receiver of the friend request.

Here is the model for a friend request/friend:

public class Friend
{
    public int Id { get; set; }
    public ApplicationUser User { get; set; }
    public ApplicationUser UserFriend { get; set; }
    public DateTime BecameFriends { get; set; }
    public bool Status { get; set; }

}

The problem is when I'm trying to display the friend requests active for a logged in user. I've got a ShowFriendRequest action that looks like this:

public ActionResult ShowFriendRequests(string Id)
{
        //ADC is the ApplicationDataContext

        var result = from f in ADC.Friends select f;
        List<Friend> SearchResult = new List<Friend>();

        SearchResult = result.Where(x => 
        x.UserFriend.Id.Equals(Id)).ToList();

        var model = new FriendViewModel
        {
            FriendList = SearchResult
            //this is a List<Friend>
        };

        return PartialView(model);
 }

So the problem here is this:

There are 2 friend requests total in the Friends table but only one that is targeted towards the logged in user. When debugging the action above it actually finds that one friend request that exists since both Friendlist and SearchResults count is 1.

Though when I try to extract some values from model.Friendlist or SearchResult either in the view or in the controller I can find the ID and status but both the User and UserFriend is null. An attempt at this might look like this:

System.Diagnostics.Debug.WriteLine(SearchResult[0].UserFriend.Id);

Why are the ApplicationUser entities null when extracting one friend request from the database?

Upvotes: 0

Views: 98

Answers (2)

Matt
Matt

Reputation: 133

TanvirArjel's answer is correct, include is mandatory if you want to perform that kind of query.

But you can rewrite all

var result = from f in ADC.Friends select f;
List<Friend> SearchResult = new List<Friend>();

//ADC is the ApplicationDataContext

SearchResult = result.Where(x => x.UserFriend.Id.Equals(Id)).ToList();

var model = new FriendViewModel
        {
            FriendList = SearchResult
            //this is a List<Friend>
        };


        return PartialView(model);

to this

var SearchResult = ADC.AspNetUsers.Select(x=>x.Friend).ToList(); //Maybe you will have FriendNavigation if you don't find Friend relationship.. use intellisense in that case

var model = new FriendViewModel
        {
            FriendList = SearchResult
            //this is a List<Friend>
        };


return PartialView(model);

Upvotes: 0

TanvirArjel
TanvirArjel

Reputation: 32079

Problem is in the following line:

var result = from f in ADC.Friends select f;

You are neither lazy loading or eager loading the ApplicationUser with Friend. So write your result query as follows:

var result = ADC.Friends.Include(f => f.ApplicationUser);

Upvotes: 2

Related Questions