public static TDestination MapTo <TSource, TDestination>(this TSource source) { //If we are mapping from Entity to Model //then we will convert all DateTime values from UTC to User timezone. //We need to mark complex properties with attribute ComplexTypeAttribute var destination = AutoMapperConfiguration.Mapper.Map <TSource, TDestination>(source); var dateTimeHelper = WebApiEngineContext.Current.Resolve <IDateTimeHelper>(); var dateTimePropertyInfos = DateTimePropertyInfoHelper.GetDatePropertyInfos(typeof(TDestination)); dateTimePropertyInfos.DateTimePropertyInfos.ForEach(property => { var dateTime = (DateTime?)property.GetValue(destination); if (dateTime.HasValue) { property.SetValue(destination, dateTimeHelper.ConvertToUserTime(dateTime.Value, DateTimeKind.Utc)); } }); dateTimePropertyInfos.ComplexTypePropertyPaths.ForEach(propertPath => { var dateTime = (DateTime?)ReflectionHelper.GetValueByPath(destination, typeof(TDestination), propertPath); if (dateTime.HasValue) { ReflectionHelper.SetValueByPath(destination, typeof(TDestination), propertPath, dateTimeHelper.ConvertToUserTime(dateTime.Value, DateTimeKind.Utc)); } }); return(destination); }
public void GetDatePropertyInfos_Test() { var dateTimePropertInfos = DateTimePropertyInfoHelper.GetDatePropertyInfos(typeof(Hotel)); dateTimePropertInfos.DateTimePropertyInfos.Count.ShouldBe(1); dateTimePropertInfos.ComplexTypePropertyPaths.Count.ShouldBe(6); dateTimePropertInfos.ComplexTypePropertyPaths.Count(path => path.Contains("RealLocation")).ShouldBe(3); dateTimePropertInfos.ComplexTypePropertyPaths.Count(path => path.Contains("VirtualLocation")).ShouldBe(3); dateTimePropertInfos.ComplexTypePropertyPaths.Count(path => path.Contains("Country")).ShouldBe(2); }
protected void ConfigureGlobalValueConverter <TEntity>(ModelBuilder modelBuilder, IMutableEntityType entityType) where TEntity : class { if (entityType.BaseType == null && !typeof(TEntity).IsDefined(typeof(DisableDateTimeNormalizationAttribute), true) && !typeof(TEntity).IsDefined(typeof(OwnedAttribute), true) && !entityType.IsOwned()) { var dateTimeValueConverter = new AbpDateTimeValueConverter(); var dateTimePropertyInfos = DateTimePropertyInfoHelper.GetDatePropertyInfos(typeof(TEntity)); dateTimePropertyInfos.DateTimePropertyInfos.ForEach(property => { modelBuilder .Entity <TEntity>() .Property(property.Name) .HasConversion(dateTimeValueConverter); }); } }
/// <summary> /// 把所有的时间数据按照指定的格式进行转换 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="modelBuilder"></param> /// <param name="entityType"></param> protected void ConfigureGlobalValueConverter <TEntity>(ModelBuilder modelBuilder, IMutableEntityType entityType) where TEntity : class { AbpDebug.WriteLine($"SelfType:{entityType},BaseType:{entityType.BaseType}"); //这里的BaseType,只有当前类有父类的时候,才是不空的。且还有个条件:该类必须是DbContext的DbSet属性。 if (entityType.BaseType == null && !typeof(TEntity).IsDefined(typeof(DisableDateTimeNormalizationAttribute), true) && !typeof(TEntity).IsDefined(typeof(OwnedAttribute), true) && !entityType.IsOwned()) //OwnsOne https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities { var dateTimeValueConverter = new AbpDateTimeValueConverter(); var dateTimePropertyInfos = DateTimePropertyInfoHelper.GetDatePropertyInfos(typeof(TEntity)); dateTimePropertyInfos.DateTimePropertyInfos.ForEach(property => { modelBuilder .Entity <TEntity>() .Property(property.Name) .HasConversion(dateTimeValueConverter); }); } }