1

私は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 を超える製品を含む「アクセサリー」カテゴリがあります。コードは、選択されたカテゴリのすべてを取得しますが、必要なときに製品を遅延ロードします。製品の読み込みが遅延しているときは、時間がかかるときです (明らかに)。これを高速化する方法を知る必要があります。誰か提案はありますか?

どうもありがとう

編集:ここにCategoryandProductクラスがあります

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;
            }
        }
4

1 に答える 1

2

あなたGet<Category>(x => x.SomeName == "abcdef")と製品の繰り返しを想像してください。

できませんGet<Product>(x => x.Categories.Where(y => y.SomeName == "abcdef").Count() > 0)か?

于 2013-01-28T17:05:00.597 に答える