繁体   English   中英

实体框架急于加载包含的功能不起作用

[英]Entity Framework eager loading with include not working

领域模型

public class InvestmentSession
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public DateTime StartDate { get; set; }

    [Required]
    public DateTime EndDate { get; set; }

    [ForeignKey("CreatedById")]
    public virtual User CreatedBy { get; set; }

    [Required]
    public int CreatedById { get; set; }

    [Required]
    public virtual Instrument Instrument { get; set; }

    public virtual ICollection<SessionUser> SessionUsers { get; set; }
}
public class SessionUser
{
    [Key, Column(Order = 1), ForeignKey("InvestmentSession")]
    public int InvestmentSession_Id { get; set; }

    [Key, Column(Order = 2), ForeignKey("User")]
    public int User_Id { get; set; }

    public virtual InvestmentSession InvestmentSession { get; set; }

    [InverseProperty("UserSessions")]
    public virtual User User { get; set; }

    public decimal? TotalProfitLoss { get; set; }
}

public class User
{
    public User()
    {

    }

    public User(string name, string email)
    {
        Name = name;
        Email = email;
        IsActive = 1;
    }

    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreatedDate { get; set; }

    [Required]
    public int IsActive { get; set; }

    [JsonIgnore]
    public virtual ICollection<SessionUser> UserSessions { get; set; }

    [ForeignKey("Role_Id")]
    [JsonIgnore]
    public virtual Role Role { get; set; }

    [Required]
    public int Role_Id { get; set; }
}

我正在尝试通过Id进行活动会话并包含SessionUsers

public InvestmentSession GetActiveSession(int id)
    {
        try
        {
            var now = DateTime.UtcNow;
            return Context.InvestmentSession.Include(s => s.SessionUsers).FirstOrDefault(i => i.Id == id && now >= i.StartDate && now < i.EndDate);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

foreach (var sessionUser in session.SessionUsers)
            {
                var key = session.Id + sessionUser.User.Email + Constants.TRADER_PERFORMANCE_CACHE_IDENTIFIER;
                var traderPerformance = RedisStore.GetValue<TraderLivePerformanceDto>(key);
                if (traderPerformance != null)
                {
                    activeSessionData.Performances.Add(traderPerformance);
                }
                else
                {
                    var newTraderPerformance = new TraderLivePerformanceDto
                    {
                        traderId = sessionUser.User_Id,
                        traderName = sessionUser.User.Name,
                        initialCash = session.InitialCash,
                        availableCash = session.InitialCash,
                        portfolioValue = session.InitialCash
                    };
                    RedisStore.StoreValue(key, newTraderPerformance, session.EndDate);
                    activeSessionData.Performances.Add(newTraderPerformance);
                }
            }

但是,在session.SessionUsers上进行迭代非常慢,在循环中添加一个断点将导致代码进入“ ExecuteReader需要打开且可用的Connection。连接的当前状态为关闭 ”,因此我假设它仍在查询DB SessionUsers。

找到了问题,在循环中访问sessionUser.User。

将我的查询更改为

return Context.InvestmentSession.Include(s => s.SessionUsers.Select(su => su.User)).FirstOrDefault(i => i.Id == id && now >= i.StartDate && now < i.EndDate);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM