我对使用Lazy加载功能的Linq to SQL非常感兴趣.而在我的项目,我用AutoMapper映射数据库模型域模型(从DB_RoleInfo到DO_RoleInfo).在我的存储库代码中如下:
public DO_RoleInfo SelectByKey(Guid Key)
{
return SelectAll().Where(x => x.Id == Key).SingleOrDefault();
}
public IQueryable<DO_RoleInfo> SelectAll()
{
Mapper.CreateMap<DB_RoleInfo, DO_RoleInfo>();
return from role in _ctx.DB_RoleInfo
select Mapper.Map<DB_RoleInfo, DO_RoleInfo>(role);
}
Run Code Online (Sandbox Code Playgroud)
SelectAll方法运行良好,但是当我调用时SelectByKey,我收到错误:
方法"RealMVC.Data.DO_RoleInfo MapDB_RoleInfo,DO_RoleInfo"无法转换为SQL.
Automapper是否完全不支持Linq?
我尝试了下面的手动映射代码而不是Automapper:
public IQueryable<DO_RoleInfo> SelectAll()
{
return from role in _ctx.DB_RoleInfo
select new DO_RoleInfo
{
Id = role.id,
name = role.name,
code = role.code
};
}
Run Code Online (Sandbox Code Playgroud)
这种方法按照我想要的方式工作.