实体框架导航属性为null

时间:2015-12-23 11:14:26

标签: entity-framework linq asp.net-mvc-4 model

我有两个使用Entity Framework的模型。

public class Player
{

    public int PlayerId { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public string Plays { get; set; }
    public string FavouriteSurface { get; set; }

}

public class SinglesMatch
{
    public int SinglesMatchId { get; set; }
    public int Player1Id { get; set; }
    public int Player2Id { get; set; }
    public int PlayerIdWinner { get; set; }
    public DateTime Date { get; set; }
    public string Venue { get; set; }
    public string Score { get; set; }
    public List<Player> Players { get; set; } 
}

我使用以下代码尝试显示播放器的名称,基于SinglesMatch模型中与Player模型中的PlayerID匹配的PlayerId。

      @foreach (var item in @Model)
                    {

                        <ul id="Players" class="bg-success"></ul>
                        <br/>
                        <h3>Date - @Html.DisplayFor(@modelItem => item.Date)</h3>
                        <li>Venue - @Html.DisplayFor(@modelItem => item.Venue)</li>
                        <li>Player 1 - @Html.DisplayFor(@modelItem => item.Players.First(p => p.PlayerId == item.Player1Id).Name)</li>
                        <li>Player 2 - @Html.DisplayFor(@modelItem => item.Players.First(p => p.PlayerId == item.Player2Id).Name)</li>
                        <li>Score- @Html.DisplayFor(@modelItem => item.Score)</li>



                    }

调试时,从我的存储库中检索模型时,导航属性始终显示为null。

我是否以正确的方式使用导航属性?我的查询有问题吗?

编辑以包含DbContext:

    public TennisTrackerContext() : base("name=TennisTrackerContext")
    {

    }

    public DbSet<Player> Players { get; set; }
    public DbSet<PlayerRecord> PlayerRecords { get; set; }
    public DbSet<SinglesMatch> SinglesMatches { get; set; }
    public DbSet<DoublesMatch> DoublesMatches { get; set; }
    public DbSet<Venue> Venues { get; set; } 
}

}

2 个答案:

答案 0 :(得分:1)

您需要添加桥接表。 Sql会自动创建,但除非您在c#中创建变量,否则您无法访问变量。

public class Player
    {
        public int PlayerId { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public string Plays { get; set; }
        public string FavouriteSurface { get; set; }
        List<PlayerInMatch> Matches { get; set; }

        public Player()
        {
            Matches = new List<PlayerInMatch>();
        }
    }

    public class PlayerInMatch
    {
        public int Id { get; set; }
        public int PlayerId { get; set; }
        [ForeignKey("PlayerId")]
        public Player Player { get; set; }
        public int SinglesMatchId { get; set; }
        [ForeignKey("SinglesMatchId")]
        public SinglesMatch SinglesMatch { get; set; }

    }

    public class SinglesMatch
    {
        public int SinglesMatchId { get; set; }
        public int PlayerIdWinner { get; set; }
        public DateTime Date { get; set; }
        public string Venue { get; set; }
        public string Score { get; set; }
        public List<PlayerInMatch> Players { get; set; }

        public SinglesMatch()
        {
            Players = new List<PlayerInMatch>();
        }
    }

    static void Main(string[] args)
    {
        var match = new SinglesMatch();
        match.Players.Select(c => c.Player.Name);
    }

答案 1 :(得分:0)

您需要设置导航属性virtual以启用延迟/急切加载:

public class SinglesMatch
{
public int SinglesMatchId { get; set; }
public int Player1Id { get; set; }
public int Player2Id { get; set; }
public int PlayerIdWinner { get; set; }
public DateTime Date { get; set; }
public string Venue { get; set; }
public string Score { get; set; }

public virtual List<Player> Players { get; set; } 
}

另外,您是否在流利的api中定义了SinglesMatchSingles之间的关系?

编辑:我看到你没有通过注释或流畅的api映射任何关系,我建议你看看这个:

https://msdn.microsoft.com/en-us/data/jj591617.aspx