c# - 使用 AutoMapper 将 ef 模型映射到 dto,DateTime 数据类型引发异常
问题描述
我正在制作 Web Api + Entity Framework 应用程序。所以我正在使用AutoMapper
将 EF 模型类映射到 DTO 类。
这是AutoMapper
重要的配置之一Apartment
。此方法在Global.asx
文件中调用
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Amenities, AmenitiesDTO>();
cfg.CreateMap<Image, ImageDTO>();
cfg.CreateMap<Location, LocationDTO>();
cfg.CreateMap<Comment, CommentDTO>();
cfg.CreateMap<Reservation, ReservationDTO>();
cfg.CreateMap<Apartment, ApartmentDTO>();
});
}
}
这是ApartmentDTO
课堂上有问题的属性。
public List<DateTime> FreeDate { get; set; }
这是Apartment
课堂上有问题的属性。
[Column(TypeName = "datetime2")]
public List<DateTime> FreeDate { get => freeDate; set => freeDate = value; }
这是我使用 Postman 定位的控制器操作。
public IQueryable<ApartmentDTO> GetApartments()
{
var apartments = db.Apartments
.Include(a => a.Amenities)
.Include(a => a.Images)
.Include(a => a.Location)
.Include(a => a.Reservations.Select(c => c.Comment)).ProjectTo<ApartmentDTO>();
return apartments;
}
我收到以下错误:
The specified type member 'FreeDate' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
所以我需要对DateTime
数据类型做一些事情,以便它可以与 DTO 一起使用并在 Postman 中获得工作响应。什么?
编辑:我猜这个问题与 in 有关List<DateTime>
,因为我有完全相同的设置DateTime
inReservationsController
并且它工作正常。
解决方案
推荐阅读
- javascript - 在一个对象中等待并返回所有已解决的承诺值
- java - Spring Boot Thymeleaf 不会解析模板
- python - 有没有办法检查和替换 1-2 行内的多个正则表达式匹配?
- python - 如何在不同的文件夹中合并2个同名的excel文件?
- ruby - 将文件从本地目录放入站点包的配方中的刀引导“没有这样的文件或目录@ dir_initialize”
- tcp - 将 unix 域套接字转发到 tcp 端口
- javascript - jquery 错误:未捕获的 TypeError:$(...).DataTable 不是函数
- bash - 如何在 sed 中对模式进行颜色匹配
- python - 运行已编译的 Python 脚本时引发了意外的 URLError
- android - Android:播放 YouTube 视频后如何返回上一个屏幕