public class MyDbContext: DbContext
{
}
public class Product
{
public long Id {get; set;}
public long CategoryId {get; set;}
[ForeignKey("CategoryId")]
public virtual Category Category {get; set;}
}
public class Category
{
public long Id {get; set;}
public string Name {get; set;}
}
List<Product> GetProducts()
{
var context = new MyDbContext();
var products = context.Set<Product>().ToList();
var categories = context.Set<Category>().ToList();
foreach(var product = in products)
{
product.Category = categories.First(c => c.Id == product.CategoryId);
}
return products;
}
在这里,我想检索所有具有最佳性能的关联类别的产品。 我第一次尝试延迟加载,但它导致了许多数据库查询。然后我使用急切的加载,但生成的查询脚本并不那么高效,特别是对于复杂的查询。所以我用下面的方法:明确加载导航属性
得到所有产品,
得到所有类别和
产品的导航属性“类别”手动设置来自取出类别
我的问题是:
- 即使在我手动设置后,EF仍会延迟加载导航属性“Category”?
- 是否有更好的解决方案来加快复杂查询的加载速度?
你真的不需要明确地说明。更改跟踪自动修复您的依赖关系。看到我的一些问题,但它也包括你需要的东西:http://stackoverflow.com/questions/17766211/how-to-do-linq-many-to-many-join-with-or-without- navigation-properties/20554719#20554719 –