c# - 使用 EfCore 执行多个 ProjectTo 层时出错
问题描述
我正在尝试为在我的公司中构建 rest api 创建一个“设计指南”,我希望拥有一个包含 automapper、asp.net 核心和 ef 核心的 3 层结构。
Database Layer : DbSet Entities
Business Layer : Application Models
Api/Rest/External Layer : Dto Object
我已经通过以下要点成功地做到了这一点:https ://gist.github.com/Angelinsky7/49c86333584e3f9cece44a88e7febd4e
但正如你所看到的,我需要使用 hack 来完成这项工作......(https://gist.github.com/Angelinsky7/49c86333584e3f9cece44a88e7febd4e#file-automapper_bug_lambda_projection-cs-L23-L24),(https://gist .github.com/Angelinsky7/49c86333584e3f9cece44a88e7febd4e#file-automapper_bug_lambda_projection-cs-L89)和(https://gist.github.com/Angelinsky7/49c86333584e3f9cece44a88e7febd4e#file-automapper_bug_lambda_projection-cs-L93)
public IEnumerable<SubLayer2> _Subs { get; set; } = new HashSet<SubLayer2>();
public ICollection<SubLayer2> Subs => (ICollection<SubLayer2>)_Subs;
cfg.CreateMap<Layer1, Layer2>()
.ForMember(p => p._Subs, opt => opt.MapFrom(src => src.Subs));
cfg.CreateMap<Layer2, Layer3>()
.ForMember(p => p.Subs, opt => opt.MapFrom(src => src._Subs));
而且我非常不喜欢它(因为即使这样工作,在更复杂的 senarii 中,它也不再工作了)
如果我不这样做:https ://gist.github.com/Angelinsky7/0e26507c07c066376d5a4de8726dd1f2
在第 3 层为 IEnumerable 的 InMemory EfCore 案例中:(在创建问题之前我没有这个)
Unhandled exception. System.InvalidOperationException: The LINQ expression 'dtoSubLayer2' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()
在使用 Layer3 作为 IEnumerable 的 SqlServer EfCore 案例中:(我想使用什么)
InvalidOperationException: When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression' must return a non-null value of the same type. Alternatively, override 'VisitLambda' and change it to not visit children of this type.
在所有以 Layer3 作为 ICollection 的 EfCore 中:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
我究竟做错了什么 ?这完全不可能吗?有没有办法让我找到弹出异常并自定义此行为的 NullReferenceException 或 VisitLambda ?拥有这种解决方案将是完美的,因为我将拥有我想要构建我的 rest api 的东西......
源/目标类型
internal class Layer1 {
public Int64 Id { get; set; }
public ICollection<SubLayer1> Subs { get; set; } = new HashSet<SubLayer1>();
}
internal class SubLayer1 {
public Int64 Id { get; set; }
public Int64 LayerId { get; set; }
}
internal class Layer2 {
public Int64 Id { get; set; }
public ICollection<SubLayer2> Subs { get; set; } = new HashSet<SubLayer2>();
}
internal class SubLayer2 {
public Int64 Id { get; set; }
}
internal class Layer3 {
public Int64 Id { get; set; }
public IEnumerable<SubLayer3> Subs { get; set; } = new HashSet<SubLayer3>();
}
internal class SubLayer3 {
public Int64 Id { get; set; }
}
映射配置
cfg.CreateMap<Layer1, Layer2>();
cfg.CreateMap<SubLayer1, SubLayer2>();
cfg.CreateMap<Layer2, Layer3>();
cfg.CreateMap<SubLayer2, SubLayer3>();
版本:xyz
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
预期行为
我应该能够多次使用 ProjectTo 来更改图层,而无需使用任何技巧。当我愿意时,我应该能够在 ICollection 和 IEnumerable 之间进行选择
实际行为
根据实现之一,3 个异常会上升(大多数情况下是“VisitLambda”lambda 之一
重现步骤
https://gist.github.com/Angelinsky7/0e26507c07c066376d5a4de8726dd1f2
感谢所有的帮助,感谢您抽出宝贵的时间!(对不起,如果这是一个 efcore 问题,但我在想:“因为我可以在 automapper 中创建一个“hack”,它可能就在这里”)
我来到这里是因为 automapper 团队认为这不是他们的错误(https://github.com/AutoMapper/AutoMapper/issues/3280)
解决方案
似乎 automapper 不能做我想做的事。我的错。感谢社区帮助我。
推荐阅读
- pyspark - Azure Synpase - PySpark 地理处理形状文件
- ios - Cocoapods 安装权限错误有点严重
- python - 从列中随机选择值,然后在 Python 的另一列中删除具有该值的行
- json - Perl:来自导入 JSON 数据的哈希,Dumper 输出正确的数据,但是我无法访问它
- python - 已经在使用fillna,但在python中仍然有空值
- python - Tensorflow Keras 交叉熵损失与输出上的线性激活
- c++ - Count number of matching bytes between two _m128i SIMD vectors
- azure - Azure 数据工厂管道中的压缩文件添加了额外的文件
- javascript - 每页有限制的成员映射
- python - Python,熊猫排除异常值函数