当调用一个显示我已扩展的ASP.NET的ApplicationUser类的属性的视图时,当代码尝试渲染我潜入ApplicationUser类的行时,我收到以下错误:
@model BaseballStatTracker.Models.ApplicationUser
@foreach (var stats in Model.GameStatistics)
{
<tr>
<td>
@Html.DisplayFor(modelItem => stat.Game.GameTime)
</td>
</tr>
}
...的原因:
从“Game”属性中访问的任何属性都会导致错误,并且Razor在编辑视图时没有指出存在问题。只需访问GameStatistics上的常规属性即可正常运行。
我有两种情况;标准的ApplicationDbContext和我的GamesContext包含Game和GameStatistics实体。我已根据以下内容覆盖了GamesContext上的OnModelCreating方法:
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);
...
}
ApplicationUser,Game和GameStatistics类如下所示:
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; }
}
游戏:
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; }
}
我已经尝试重做OnModelCreate方法,但我无法看到我对Game&lt; -GameStatistics关系做错了什么。任何帮助将非常感激。提前谢谢 -
答案 0 :(得分:0)
在HomeTeam和AwayTeam属性上为我的游戏模型添加了几个“反向属性”注释后,此问题得以解决。我也不再使用OnModelCreating覆盖并切换到完全使用注释。代码更改的相关部分是:
[Display(Name = "Home Team")]
public virtual Guid? HomeTeamId { get; set; }
[ForeignKey("HomeTeamId")]
[InverseProperty("HomeGames")] // Added this -
public virtual Team HomeTeam { get; set; }
[Display(Name = "Away Team")]
public virtual Guid? AwayTeamId { get; set; }
[ForeignKey("AwayTeamId")]
[InverseProperty("AwayGames")] // Added this -
public virtual Team AwayTeam { get; set; }
我不确定为什么我无法在数据上下文中设置相同的结果 - 我确信这只是我的误解,但我很想知道原因。如果EF通过配置不知道逆特性是什么?
我曾尝试更改配置设置但没有效果 - 但是我首先看不出它有什么问题。尽管如此,它现在正在使用数据注释。