Beispiel #1
0
 private void PrintPropertyValues(System.Data.Entity.Infrastructure.DbPropertyValues currentValues, StreamWriter sw)
 {
     foreach (var item in currentValues.PropertyNames)
     {
         var valor = currentValues[item];
         sw.WriteLine(item + ": " + valor);
     }
 }
        //public static T DeepClone<T>(T toClone)
        //{
        //    T toCloneInto = default(T);
        //    var properties = toClone.GetType().GetProperties();
        //    foreach (var property in properties)
        //    {
        //        var value = property.GetValue(toClone);
        //        property.SetValue(toCloneInto, value);
        //    }
        //    return toCloneInto;
        //}

        public static T CreateWithValues <T>(System.Data.Entity.Infrastructure.DbPropertyValues values)
            where T : new()
        {
            T    entity = new T();
            Type type   = typeof(T);

            foreach (var name in values.PropertyNames)
            {
                var property = type.GetProperty(name);
                property.SetValue(entity, values.GetValue <object>(name));
            }

            return(entity);
        }
Beispiel #3
0
        public bool GetIsReallyChanged(bool ForceDetectChanges = false)
        {
            // cache original values
            if (ReferenceEquals(_originalValues, null))
            {
                DBContextBase ctx      = null;
                bool          oldState = true; // if something goes wrong it will force TRUE, it can slow down program, but cant break anything
                try{
                    ctx = GetCtx();
                    if (ctx == null)
                    {
                        throw new ApplicationException("GetIsReallyChanged need db context!");
                    }

                    // defaultly off
                    oldState = ctx.Configuration.AutoDetectChangesEnabled;
                    ctx.Configuration.AutoDetectChangesEnabled = ForceDetectChanges;

                    System.Data.Entity.Infrastructure.DbEntityEntry <EntityBase> _Entry = ctx.Entry(this as EntityBase);
                    if (ReferenceEquals(_Entry, null) || ReferenceEquals(_Entry.OriginalValues, null))
                    {
                        throw new ApplicationException("GetIsReallyChanged can't identify entity in db contet!");
                    }
                    _originalValues = _Entry.OriginalValues;
                }finally{
                    // be sure autodetect is on
                    if (!ReferenceEquals(ctx, null))
                    {
                        ctx.Configuration.AutoDetectChangesEnabled = oldState;
                    }
                }

                if (ReferenceEquals(_originalValues, null))
                {
                    throw new ApplicationException("GetIsReallyChanged can't work without original values!");
                }
            }


            /* just use original values and manually test */
            foreach (string en in _originalValues.PropertyNames)
            {
                if (!Equals(_originalValues[en], GetPropertyValue(en)))
                {
                    return(true);
                }
            }
            // not changed
            return(false);
        }
Beispiel #4
0
 public static object GetDbValue(this System.Data.Entity.Infrastructure.DbPropertyValues propertyValues, string propertyName)
 {
 }