EF 6添加包含导航属性的子句

时间:2014-08-13 02:07:36

标签: c# entity-framework entity-framework-5

我会尝试在不发布模型中的所有对象的情况下提出这个问题。我有一个有点复杂的查询,但只有两个对象与问题有关。

我有一个用来办公室足球池的网站。所以我的域模型有Team和TeamRecords

以下是定义。我删除了对象上的一些不相关的属性。

public class Team
{
    /// <summary>
    /// Team ID
    /// </summary>
    public int TeamID { get; set; }


    /// <summary>
    /// Team Recordproperty
    /// </summary>
    public virtual ICollection<TeamRecord> TeamRecords { get; set; }
}

public class TeamRecord
{

    /// <summary>
    /// Team ID
    /// </summary>
    public int TeamID { get; set; }

    /// <summary>
    /// Team the record belongs to
    /// </summary>
    public virtual Team Team { get; set;}

    /// <summary>
    /// Season ID
    /// </summary>
    public int SeasonID { get; set; }

    /// <summary>
    /// Season navigation property
    /// </summary>
    public virtual Season Season { get; set; }

}

我将Team配置为TeamRecords关系,如下所示:

HasMany(t => t.TeamRecords).WithRequired(tr => tr.Team).HasForeignKey(tr=>new {tr.TeamID});

然后我尝试运行这样的查询。基本上当我选择一支球队时,我只想选择本赛季的球队记录。所以我想在我的Include方法中添加一个where子句。忽略查询中的其他对象。他们可能是自我解释的。

var picks = context.Picks.Where(p => ((p.Game.SeasonID == seasonID) && (p.Game.Week ==     week) && (p.PoolID == poolID) && (p.UserID == userID)))
                            .Include(p => p.Game).Include(p => p.Game.HomeTeam).Include(p => p.Game.VisitingTeam).Include(p => p.Pool)
                            .Include(p => p.Game.HomeTeam.TeamRecords.Where(tr=>tr.SeasonID == seasonID))
                            .Include(p => p.Game.VisitingTeam.TeamRecords.Where(tr=>tr.SeasonID == seasonID))
                            .Select(p => p);

当我执行该行代码时,出现以下错误

Include路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。

如何完成此类过滤?我在互联网上搜索过,没有运气。

2 个答案:

答案 0 :(得分:11)

您可以将其更改为生成匿名类型的select语句,然后执行查询并再次选择根对象。

你可以尝试这样的事情。

var picks = context.Picks.Where(p => ((p.Game.SeasonID == seasonID) && (p.Game.Week == week) && (p.PoolID == poolID) && (p.UserID == userID)))
    .Select(p => new
    {
        Pick = p,
        Game = p.Game,
        HomeTeam = p.Game.HomeTeam,
        VisitingTeam = p.Game.VisitingTeam,
        HomeTeamRecords = p.Game.HomeTeam.TeamRecords.Where(tr => tr.SeasonID == seasonID),
        VisitingTeamRecords = p.Game.VisitingTeam.TeamRecords.Where(tr => tr.SeasonID == seasonID),
        Pool = p.Pool
    })
    .ToArray().Select(p => p.Pick).ToArray();

他们会自动关联,PickGameGameHomeTeamGameVisitingTeamHomeTeamTeamRecordsVisitingTeamTeamRecordsPickPool

也称为 relationship fix-up

答案 1 :(得分:5)

实体框架有no direct support for filtering the Include extension method

常见的替代方法是在跟踪的实体上合并QueryLoad方法以执行filtered explicit load。 (在该链接文章上显式加载相关实体时搜索应用过滤器。)

仅使用您的TeamTeamRecords实体,这看起来像这样:

// Ensure lazy loading is turned off
context.Configuration.LazyLoadingEnabled = false;

// load the team
var team = context.Teams.Find(teamId);

// explicitly load the team records for the current season
context.Entry(team)
    .Collection(t => t.TeamRecords)
    .Query()
    .Where(r => r.SeasonId == currentSeasonId)
    .Load();