实体框架中的性能问题

时间:2018-02-22 15:12:15

标签: mysql .net-core entity-framework-core dbcontext asp.net-core-webapi

我有一些数据库模型:

 public class SomeEntity
    {
        [Key]
        public int Id { get; set; }
        public Schedule Schedule { get; set; }
        public ICollection<Address> Addresses { get; set; }
        public ICollection<Phone> Phones { get; set; }
        public ICollection<Email> Emails { get; set; }
    }

public class Schedule
{
    [Key]
    public int id { get; set; }
    public ICollection<TimeRange> Monday { get; set; }
    public ICollection<TimeRange> Tuesday { get; set; }
    public ICollection<TimeRange> Wednesday { get; set; }
    public ICollection<TimeRange> Thursday { get; set; }
    public ICollection<TimeRange> Friday { get; set; }
    public ICollection<TimeRange> Saturday { get; set; }
    public ICollection<TimeRange> Sunday { get; set; }
}

当我跑步时:

 var entity = _dbContext.SomeEntity
                .Include(p => p.Addresses)
                .Include(p => p.Emails)
                .Include(p => p.Phones)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Monday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Tuesday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Wednesday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Thursday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Friday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Saturday)
                .Include(p => p.Schedule)
                .ThenInclude(s => s.Sunday)
            return entity;

执行需要很长时间。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的包含太多了。目前没有选项可以直接对其进行优化(包括过滤器将在下一版本的ef核心2.1中)。如果您确实需要所有这些数据,则应使用rawquery。