【发布时间】:2021-05-31 10:55:35
【问题描述】:
有没有办法用 AutoMapper 映射这些EntityFramework 实体?
我在尝试将 DateTime 对象(来自我的 DBContext 实体)映射到我的 DTO 上的 TimeSpan 属性时遇到错误。
这是个例外
----------- Exception #0 -----------
Type: System.NotSupportedException
Message: The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Source: EntityFramework
它曾经在ToList()调用后映射实体时工作,但现在使用AutoMapper的ProjectTo<> IQueryable扩展,它显然试图将表达式转换为SQL可以理解的表达式。
我的问题 - 是否可以在服务器上执行查询后配置某些对象上的映射? (例如在ToList() 通话之后)
DB 实体上的 CloseTime 属性是 DateTime 对象,但我们正在映射到 TimeSpan 对象
现在 - 我只是忽略了这样的属性
cfg.CreateMap<CustomerShipTo, ShipToBase>()
.ForMember(desc => desc.OpenTime, src => src.Ignore())
//.ForMember(desc => desc.OpenTime, src => src.CloseTime.TimeOfDay)
.ReverseMap();
【问题讨论】:
-
你遇到了什么错误?
-
您能否分享模型、您在哪里使用
ProjectTo的查询以及您现在收到的错误消息。这真的会帮助其他人了解您的问题。 -
请注意这里可能的 X/Y 情况:
My question - is it possible to configure mapping on certain objects to take place after the query has been executed on the server? (e.g. after the ToList() call)真正的问题是:“尝试将 DateTime 对象(从我的 DBContext 实体)映射到 TimeSpan 时出现错误我的 DTO 上的财产。” -
添加了异常细节...
标签: c# entity-framework automapper iqueryable