public void DateTimeKindAttribute_Apply_ParameterIsNotNull_DateTimeIsNull_DoesNothing()
        {
            DateTimeKindAttribute converter = new DateTimeKindAttribute(DateTimeKind.Utc);

            Assert.True(converter.Kind == DateTimeKind.Utc);

            DateTime          dt = new DateTime(2018, 1, 1, 12, 30, 00, DateTimeKind.Local);
            DateTimeTestClass dateTimeTestClass = new DateTimeTestClass(dt);

            DateTimeKindAttribute.Apply(dateTimeTestClass);
        }
Example #2
0
        private static Action <T> CreateActionForProperty(PropertyInfo property, DateTimeKindAttribute attribute)
        {
            var kind = attribute.Kind;

            if (property.PropertyType == typeof(DateTime?))
            {
                //creates delegates only once, before the real execution
                var getDatetimeNullable = CreateGetterDelegate <DateTime?>(property);

                var setDatetimeNullable = CreateSetterDelegate <DateTime?>(property);

                //You can place a breakpoint inside the action below
                return((T entity) =>
                {
                    var date = getDatetimeNullable(entity);
                    if (date == null)
                    {
                        return;
                    }

                    setDatetimeNullable(entity, DateTime.SpecifyKind(date.Value, kind));
                });
            }
            else if (property.PropertyType == typeof(DateTime))
            {
                //creates delegates only once, before the real execution
                var getDatetime = CreateGetterDelegate <DateTime>(property);
                var setDatetime = CreateSetterDelegate <DateTime>(property);

                //You can place a breakpoint inside the action below
                return((T entity) =>
                {
                    var date = getDatetime(entity);

                    setDatetime(entity, DateTime.SpecifyKind(date, kind));
                });
            }
            else
            {
                throw new NotSupportedException("DateTimeKindAttribute - property type: " + property.PropertyType.FullName);
            }
        }
Example #3
0
        public ApplicationDbContext()
            : base("ApplicationDbContext", throwIfV1Schema: false)
        {
            // http://www.farreachinc.com/blog/far-reach/2013/09/26/entity-framework-query-optimizations
            this.Configuration.LazyLoadingEnabled = true; // 手動指定(不確定預設值是?)
            //this.Configuration.LazyLoadingEnabled = false; // 20151202 Norman 測試效能,暫時關閉

            // 當遇上無法理解的 EntityFramework 錯誤
            // (1) 請在發生錯誤的地方前後下中斷點
            // (2) 打開輸入視窗(顯示輸出來源選擇[偵錯])
            // (3) 觀察輸入訊息,應該就可以找到問題發生原因
            this.Database.Log = (message) => { System.Diagnostics.Debug.WriteLine(message); }; // 20151122 Norman 測試效能,暫時關閉
            this.SetCommandTimeOut(600);                                                       // 600 seconds

            // http://stackoverflow.com/questions/4648540/entity-framework-datetime-and-utc
            ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized +=
                (sender, e) => DateTimeKindAttribute.Apply(e.Entity);

            //ObjectQuery<Blacklist> query = ((IObjectContextAdapter)this).ObjectContext.CreateQuery<Blacklist>("ccc");
            //query.MergeOption = MergeOption.NoTracking;

            // this.Blacklists.MergeOption
        }
Example #4
0
 public MyContext()
 {
     ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized +=
         (sender, e) => DateTimeKindAttribute.Apply(e.Entity);
 }
Example #5
0
        public MkDataContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            Database.SetInitializer<MkDataContext>(null);
            Configuration.ProxyCreationEnabled = true;
            Configuration.LazyLoadingEnabled = true;

            // Sets DateTimeKinds on DateTimes of Entities, so that Dates are automatically set to be UTC.
            // Currently only processes CleanEntityBase entities. All EntityBase entities remain unchanged.
            // http://stackoverflow.com/questions/4648540/entity-framework-datetime-and-utc
            ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, e) => DateTimeKindAttribute.Apply(e.Entity);
        }
Example #6
0
 public static void ConfigureForDateTimeKind(this DbContext context, DateTimeKind?defaultKind = null)
 {
     context.GetObjectContext().ObjectMaterialized += (sender, e) => DateTimeKindAttribute.Apply(e.Entity, defaultKind);
 }
        public void DateTimeKindAttribute_Constructor_Default()
        {
            DateTimeKindAttribute converter = new DateTimeKindAttribute(DateTimeKind.Utc);

            Assert.True(converter.Kind == DateTimeKind.Utc);
        }