C# 使用AutoMapper将ef模型映射到dto,DateTime数据类型引发异常
我正在制作WebAPI+实体框架应用程序。所以我使用C# 使用AutoMapper将ef模型映射到dto,DateTime数据类型引发异常,c#,entity-framework,asp.net-web-api,automapper,C#,Entity Framework,Asp.net Web Api,Automapper,我正在制作WebAPI+实体框架应用程序。所以我使用AutoMapper将EF模型类映射到DTO类 这是AutoMapper的配置,重要的是单元。此方法在Global.asx文件中调用 public class AutoMapperConfiguration { public static void Configure() { Mapper.Initialize(cfg => { cfg.CreateMap<A
AutoMapper
将EF模型类映射到DTO类
这是AutoMapper
的配置,重要的是单元
。此方法在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>();
});
}
}
这是公寓
类中有问题的属性
public List<DateTime> FreeDate { get; set; }
[Column(TypeName = "datetime2")]
public List<DateTime> FreeDate { get => freeDate; set => freeDate = value; }
因此,我需要对DateTime
数据类型做一些处理,以便它可以与DTO一起使用,并在Postman中获得工作响应。什么
编辑:我猜这个问题与
List
有关,因为我在ReservationsController
中对DateTime
进行了完全相同的设置,而且效果很好。你如何看待sqldatetime2
到c列表的映射呢?@SamAxe我不知道。请你详细说明一下,你的问题对回答我的问题有什么帮助?现在我知道sqldatetime2
将映射到DateTime
c。因此,这意味着我不知道如何制作sql列表datetime2
?@SamAxe它很好地发挥了作用,在某种程度上,我学会了不能将列表存储在sql数据库中。所以要存储日期列表,我需要将其设置为csv日期字符串或类似的内容,对吗?感谢该体系结构取决于您将如何使用数据。如果您不需要查询它。。(例如,它纯粹是为了显示的目的)然后存储它,但是对于您的显示技术来说是有意义的。但是,如果您需要查询这些日期,或者比较它们,或者格式化它们,或者其他任何事情,那么它们需要采用有利于这些操作的形式。这通常意味着在与主数据的一对多关系的多个方面有一个新表。但如果您还不知道,我强烈建议您读一本书或上一堂课,教您正确的数据建模。@SamAxe谢谢,可以
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.