【发布时间】:2018-07-10 12:25:52
【问题描述】:
我正在使用存储库模式,所以我的存储库只知道 DTO。它必须使用实体框架通过一些过滤器查询数据库。 我的问题是 Entity Framework 只知道 DB 模型类,所以我必须“自动映射”Expression,然后才能在任何查询中使用它们。
我已经声明了一个接受 Expression 作为过滤器的方法。
public interface IRepository
{
IEnumerable<ItemDTO> GetItemsWithFilter(Expression<Func<ItemDTO, bool>> filter)
{
var filterDb = Mapper.Map<Expression<Func<ItemDB, bool>>>(filter);
return dbContext.CONFIGURATIONS.Where(filterDb).Select(x => Mapper.Map<ItemDTO>(x));
}
}
public class ItemDTO
{
public int numero { get; set; }
public string name { get; set; }
}
public class ItemDB //they are both the same, just for testing purpose
{
public int numero { get; set; }
public string name { get; set; }
}
//failing code
Repository.GetItemsWithFilter(x => x.name=="a");
我关注了tutorial,说可以在表达式之间进行映射,但我遇到了一些错误:
“LINQ to Entities 不支持指定的类型成员‘name’。仅支持初始化程序、实体成员和实体导航属性。”}
【问题讨论】:
-
我已经检查过了,但没有让它工作,我不知道为什么
-
考虑发布minimal reproducible example。当前提供的代码无法生成异常,因为发布的实体/DTO 模型中的任何位置都没有
Description属性。 -
@IvanStoev 你是对的。我试图发布最少的代码,但我输入了错误的变量名。现在我已经修好了
标签: entity-framework expression automapper