Entity Framework dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
Entity Framework dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
我应该如何将此查询转换为有效的 Entity Framework 查询?
dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
Where 子句抛出异常:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
我已经尝试过第三方库 IncludeFilter,它有效。但是它不允许我使用另一个包括:
dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false)).Include(x=>x.Demand)
非常感谢:-)
Include 只是告诉 EF 预先加载相关实体的标记。它不支持预先加载选定的亲属子集,例如活动项目。
使用软删除模型时 (IsDeleted/IsActive) 将使用活动子项填充视图模型。为读取操作返回视图模型的好处是您可以避免潜在的延迟加载情况,而只需检索您需要使用的数据。对于编辑操作,我会预先加载相关数据,并且通常需要活动和非活动记录。
var viewModels = dbset.Select(x => new MyVM
{
Objectid = x.ObjectId,
// ...
Offer = new OfferViewModel
{
OfferId = x.Offer.OfferId,
// ...
Images = x.Offer.Images
.Where(i => !i.IsDeleted) // Load only active images
.Select(i => new ImageViewModel
{
ImageId = i.ImageId,
//...
}).ToList()
}
}
请注意,这不需要显式 .Include()
语句,只需加载您需要的数据。
当我去保存对实体图的更改时,我会用 .Include(x => x.Order).Include(x => x.Order.Images)
加载根实体以确保我包含所有相关数据。 EF 应该在应用更新之前了解完整集合,以避免出现重复记录插入尝试等意外情况。
I already tried third party library IncludeFilter, which works. However it doesn't allow me to use another include:
开始使用IncludeFilter时,即使不过滤也需要一直使用下去。
dbSet.IncludeFilter(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
.IncludeFilter(x=>x.Demand)
我应该如何将此查询转换为有效的 Entity Framework 查询?
dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
Where 子句抛出异常:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
我已经尝试过第三方库 IncludeFilter,它有效。但是它不允许我使用另一个包括:
dbSet.Include(x=>x.Offer.Images.Where(y=>y.IsDeleted == false)).Include(x=>x.Demand)
非常感谢:-)
Include 只是告诉 EF 预先加载相关实体的标记。它不支持预先加载选定的亲属子集,例如活动项目。
使用软删除模型时 (IsDeleted/IsActive) 将使用活动子项填充视图模型。为读取操作返回视图模型的好处是您可以避免潜在的延迟加载情况,而只需检索您需要使用的数据。对于编辑操作,我会预先加载相关数据,并且通常需要活动和非活动记录。
var viewModels = dbset.Select(x => new MyVM
{
Objectid = x.ObjectId,
// ...
Offer = new OfferViewModel
{
OfferId = x.Offer.OfferId,
// ...
Images = x.Offer.Images
.Where(i => !i.IsDeleted) // Load only active images
.Select(i => new ImageViewModel
{
ImageId = i.ImageId,
//...
}).ToList()
}
}
请注意,这不需要显式 .Include()
语句,只需加载您需要的数据。
当我去保存对实体图的更改时,我会用 .Include(x => x.Order).Include(x => x.Order.Images)
加载根实体以确保我包含所有相关数据。 EF 应该在应用更新之前了解完整集合,以避免出现重复记录插入尝试等意外情况。
I already tried third party library IncludeFilter, which works. However it doesn't allow me to use another include:
开始使用IncludeFilter时,即使不过滤也需要一直使用下去。
dbSet.IncludeFilter(x=>x.Offer.Images.Where(y=>y.IsDeleted == false))
.IncludeFilter(x=>x.Demand)