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