我正在尝试利用Ardalis.Specification库在我的 asp.net 6 项目中应用规范模式。
安装库后,我创建了以下规范
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if (ids == null || !ids.Any())
{
return;
}
Query.Where(x => ids.Contains(x.Id));
// some how I need to map Product to ProductMenuItem so only the needed columns are pulled from the database.
}
}
Run Code Online (Sandbox Code Playgroud)
我不想Product从数据库中提取每个值,而是只想通过将数据投影到ProductMenuItem. 上述规范返回以下错误
SelectorNotFoundException Ardalis.Specification.SelectorNotFoundException:规范必须定义选择器
如何定义实体(即Product)和结果对象(即ProductMenuItem)之间的映射?
我尝试添加Select()定义但给了我同样的错误
public class ProductByIdsSpec : Specification<Product, ProductMenuItem>
{
public ClientRecordByIdsSpec(IEnumerable<int> ids)
{
if …Run Code Online (Sandbox Code Playgroud) c# specifications specification-pattern asp.net-core ardalis-specification