コード例 #1
0
        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);
        }
コード例 #2
0
        public virtual void Initialize(AbpEfDbContextInitializationContext initializationContext)
        {
            var uowOptions = initializationContext.UnitOfWork.Options;

            if (uowOptions.Timeout.HasValue && !Database.CommandTimeout.HasValue)
            {
                Database.CommandTimeout = uowOptions.Timeout.Value.TotalSeconds.To <int>();
            }

            if (Clock.SupportsMultipleTimezone)
            {
                ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, args) =>
                {
                    var entityType = ObjectContext.GetObjectType(args.Entity.GetType());

                    Configuration.AutoDetectChangesEnabled = false;
                    var previousState = Entry(args.Entity).State;

                    DateTimePropertyInfoHelper.NormalizeDatePropertyKinds(args.Entity, entityType);

                    Entry(args.Entity).State = previousState;
                    Configuration.AutoDetectChangesEnabled = true;
                };
            }
        }
コード例 #3
0
ファイル: EfUnitOfWork.cs プロジェクト: kblwxl/Larva
        private static void ObjectContext_ObjectMaterialized(DbContext dbContext, ObjectMaterializedEventArgs e)
        {
            var entityType    = ObjectContext.GetObjectType(e.Entity.GetType());
            var previousState = dbContext.Entry(e.Entity).State;

            DateTimePropertyInfoHelper.NormalizeDatePropertyKinds(e.Entity, entityType);

            dbContext.Entry(e.Entity).State = previousState;
        }
コード例 #4
0
        private static void ObjectContext_ObjectMaterialized(DbContext dbContext, ObjectMaterializedEventArgs e)
        {
            var entityType = ObjectContext.GetObjectType(e.Entity.GetType());

            dbContext.Configuration.AutoDetectChangesEnabled = false;

            DateTimePropertyInfoHelper.NormalizeDatePropertyKinds(e.Entity, entityType);

            dbContext.Entry(e.Entity).State = EntityState.Unchanged;
            dbContext.Configuration.AutoDetectChangesEnabled = true;
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        private void ObjectContext_ObjectMaterialized([NotNull] DbContext dbContext, ObjectMaterializedEventArgs e)
        {
            Type entityType = ObjectContext.GetObjectType(e.Entity.GetType());

            dbContext.Configuration.AutoDetectChangesEnabled = false;
            EntityState previousState = dbContext.Entry(e.Entity).State;

            DateTimePropertyInfoHelper.NormalizeDatePropertyKinds(e.Entity, entityType);

            dbContext.Entry(e.Entity).State = previousState;
            dbContext.Configuration.AutoDetectChangesEnabled = true;
        }
コード例 #7
0
 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);
         });
     }
 }
コード例 #8
0
        /// <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);
                });
            }
        }
コード例 #9
0
        private static void ObjectContext_ObjectMaterialized(object sender, ObjectMaterializedEventArgs e)
        {
            var entityType = ObjectContext.GetObjectType(e.Entity.GetType());

            DateTimePropertyInfoHelper.NormalizeDatePropertyKinds(e.Entity, entityType);
        }