paulpitchford
paulpitchford

Reputation: 560

EF6 Code First MVC Related Entity null

I have these two classes:

public class Players
{
    [Key]
    public int Id { get; set; }
    public string PlayerName { get; set; }

    public virtual ICollection<ResultItems> ResultItems { get; set; }
}

public class ResultItems
{
    [Key]
    public int Id { get; set; }
    public int ResultsId { get; set; }
    public Results Results { get; set; }
    public int Position { get; set; }
    public int PlayerId { get; set; }
    public Players Player { get; set; }
    public double Cash { get; set; }
    public int Points { get; set; }
}

You'll see here that the database is populating nice as we enter ResultItems:

screenshot

However, when the page loads, and we loop through the model to create a table of data, the Player entity is null:

screenshot

Can someone help me understand why it isn't loading? any help would be appreciated.

Thanks,

Paul.

Upvotes: 1

Views: 222

Answers (1)

Moe Bataineh
Moe Bataineh

Reputation: 1080

You need to add the virtual keyword to your public Players Player or you can do db.ResultItems.Include("Player").ToList()

Upvotes: 2

Related Questions