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

jef*_*non 8 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; }

}
Run Code Online (Sandbox Code Playgroud)

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

HasMany(t => t.TeamRecords).WithRequired(tr => tr.Team).HasForeignKey(tr=>new {tr.TeamID});
Run Code Online (Sandbox Code Playgroud)

然后我尝试运行这样的查询.基本上当我选择一支球队时,我只想选择本赛季的球队记录.所以我想在我的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);
Run Code Online (Sandbox Code Playgroud)

当我执行该行代码时,我收到以下错误

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

我怎样才能完成这种过滤?我在网上搜索过,没有运气.

Yul*_*dra 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();
Run Code Online (Sandbox Code Playgroud)

他们会自动连接PickGame,GameHomeTeam,GameVisitingTeam,HomeTeamTeamRecords,VisitingTeamTeamRecords,PickPool.

也被称为关系修复.


Mat*_*ton 5

实体框架没有直接支持过滤Include扩展方法.

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

以只是你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();
Run Code Online (Sandbox Code Playgroud)