【问题标题】:Entity Framework 4 Code First Many-to-Many relationship slowEntity Framework 4 Code First 多对多关系慢
【发布时间】:2013-01-12 00:36:48
【问题描述】:

我有一个Category 对象和一个Product 对象。它们具有多对多关系,因此在初始化数据库时会创建 CategoryProduct 表。在OnModelCreating 方法中,我有以下代码来映射关系

modelBuilder.Entity<Category>()
    .HasMany( c => c.Products )
    .WithMany( i => i.Categories )
    .Map( t => t.MapLeftKey( "CategoryId" )
    .MapRightKey( "ProductId" )
    .ToTable( "CategoryProducts" ));

CategoryProducts 表已正确加载,一切正常。但是当我实际调试站点时,导航到一个类别需要很长时间。例如,有一个包含 1400 多种产品的“配件”类别。该代码将获取所选类别的所有内容,但它会在需要时延迟加载产品。延迟加载产品时需要很长时间(显然)。我需要知道如何加快速度。有人有什么建议吗?

非常感谢

编辑:这里是 CategoryProduct

public class Category : WebPage
    {
        private int _count = -1;
        public bool IsFeatured { get; set; }
        public virtual Category Parent { get; set; }

        public virtual List<Category> Children { get; set; }
        public virtual List<Product> Products { get; set; }    
        public virtual List<Discount> Discounts { get; set; }
}

public class Product : WebPage
    {
        public string Sku { get; set; }
        public string Details { get; set; }
        public string AdditionalDetails { get; set; }    

        public virtual List<Category> Categories { get; set; }
        public virtual Brand Brand { get; set; }
}

编辑:执行查询的代码

public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "", int? limit = null)
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (limit != null && limit.HasValue && limit.GetValueOrDefault() > 0)
            {
                query = query.Take(limit.Value);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query;
            }
        }

【问题讨论】:

  • 问题不在这里,在你使用CategoryProducts的地方,你应该显示更多代码
  • 不看代码就不能说太多,但如果您只是出于只读目的而拉取数据,您可以将.AsNoTracking() 应用于您的读取以加快速度
  • 你想看什么代码?我将把 CategoryProduct 类放在上面
  • 我认为“包含方法”msdn.microsoft.com/en-us/library/bb738708.aspx 可能是您正在寻找的,通过使用包含您基本上是在告诉实体框架在第一次查询执行中检索所需的字段而不是懒惰-加载它们
  • @BahadorIzadpanah 那不会仍然使查询变慢吗?

标签: c# entity-framework-4 ef-code-first code-first


【解决方案1】:

我想你 Get&lt;Category&gt;(x =&gt; x.SomeName == "abcdef") 和产品迭代。

你不能Get&lt;Product&gt;(x =&gt; x.Categories.Where(y =&gt; y.SomeName == "abcdef").Count() &gt; 0) 吗?

【讨论】:

  • 这几乎就是我所做的......谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多