When calling a view which displays properties for ASP.NET's ApplicationUser class which I have extended, I receive the below error when the code tries to render a line where I dive into a ApplicationUser class:

@model BaseballStatTracker.Models.ApplicationUser

@foreach (var stats in Model.GameStatistics)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => stat.Game.GameTime)
        </td>
    </tr>
}

...causes:

Error Upon Rendering "Game" Navigation Properties

Any property accessed from within the "Game" property causes the error, and Razor gives no indication that there is a problem when editing the view. Just accessing regular properties on the GameStatistics works without issue.

I have two contexts; the standard ApplicationDbContext and my GamesContext which houses both the Game and GameStatistics entities. I have overridden the OnModelCreating method on the GamesContext per the following:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Game>().HasKey(i => i.GameId);
            modelBuilder.Entity<Game>().HasOptional(i => i.HomeTeam).WithMany(i => i.HomeGames).HasForeignKey(i => i.HomeTeamId);
            modelBuilder.Entity<Game>().HasOptional(i => i.AwayTeam).WithMany(i => i.AwayGames).HasForeignKey(i => i.AwayTeamId);
            modelBuilder.Entity<Game>().HasOptional(i => i.Diamond).WithMany(i => i.Games).HasForeignKey(i => i.DiamondId);

            modelBuilder.Entity<GameStatistics>().HasKey(i => i.GameStatisticsId);
            modelBuilder.Entity<GameStatistics>().HasRequired(i => i.Game).WithMany(i => i.GameStatistics).HasForeignKey(i => i.GameId);
            modelBuilder.Entity<GameStatistics>().HasRequired(i => i.Player).WithMany(i => i.GameStatistics).HasForeignKey(i => i.PlayerId);
...
}

The ApplicationUser, Game and GameStatistics classes look like the following:

ApplicationUser:

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        [Required]
        [Display(Name = "First Name")]
        [StringLength(155, ErrorMessage = "First Name can only be max 155 characters in length.")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        [StringLength(155, ErrorMessage = "Last Name can only be max 155 characters in length.")]
        public string LastName { get; set; }

        [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return FirstName + " " + LastName;
            }
        }

        [Display(Name = "Player Number")]
        public int PlayerNumber { get; set; }

        [Display(Name = "Team")]
        public virtual Guid TeamId { get; set; }
        public virtual Team Team { get; set; }

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

Game:

public class Game
    {
        [Key]
        public Guid GameId { get; set; }

        public string GameName { get
            {
                return AwayTeam.Name + " @ " + HomeTeam.Name + ", " + GameDate.Value.Date.ToLongDateString();
            } }

        [DataType(DataType.Date)]
        [Display(Name = "Game Date")]
        public DateTime? GameDate { get; set; }

        [DataType(DataType.Time)]
        [Display(Name = "Game Time")]
        public DateTime? GameTime { get; set; }

        [Display(Name = "Game Location")]
        public virtual Guid? DiamondId { get; set; }
        public virtual Diamond Diamond { get; set; }

        [Display(Name = "Home Team")]
        public virtual Guid? HomeTeamId { get; set; }
        public virtual Team HomeTeam { get; set; }

        [Display(Name = "Away Team")]
        public virtual Guid? AwayTeamId { get; set; }
        public virtual Team AwayTeam { get; set; }

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

GameStatistics:

public class GameStatistics
    {
        [Key]
        public Guid GameStatisticsId { get; set; }

        public Guid GameId { get; set; }
        public virtual Game Game { get; set; }

        public string PlayerId { get; set; }
        public virtual ApplicationUser Player { get; set; }

        public int AtBats { get; set; }

        public int Hits { get; set; }

        public int Walks { get; set; }
    }

I have tried redoing the OnModelCreate method but I can't see what I have done wrong with the Game<-GameStatistics relationship. Any help would be much appreciated. Thank you in advance -

Related posts

Recent Viewed