public static ObjectStateEntry AttachAsModifiedInternal <TEntity>(TEntity current, TEntity original, ObjectContext objectContext)
        {
            ObjectStateEntry stateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(current);

            stateEntry.ApplyOriginalValues(original);

            // For any members that don't have RoundtripOriginal applied, EF can't determine modification
            // state by doing value comparisons. To avoid losing updates in these cases, we must explicitly
            // mark such members as modified.
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TEntity));
            AttributeCollection          attributes = TypeDescriptor.GetAttributes(typeof(TEntity));
            bool isRoundtripType = attributes[typeof(RoundtripOriginalAttribute)] != null;

            foreach (var fieldMetadata in stateEntry.CurrentValues.DataRecordInfo.FieldMetadata)
            {
                string             memberName = stateEntry.CurrentValues.GetName(fieldMetadata.Ordinal);
                PropertyDescriptor property   = properties[memberName];
                // TODO: below we need to replace ExcludeAttribute logic with corresponding
                // DataContractMember/IgnoreDataMember logic
                if (property != null &&
                    (property.Attributes[typeof(RoundtripOriginalAttribute)] == null && !isRoundtripType)
                    /* && property.Attributes[typeof(ExcludeAttribute)] == null */)
                {
                    stateEntry.SetModifiedProperty(memberName);
                }
            }

            return(stateEntry);
        }
Beispiel #2
0
        public static ObjectStateEntry AttachAsModifiedInternal(object current, object original, ObjectContext objectContext)
        {
            ObjectStateEntry stateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(current);

            stateEntry.ApplyOriginalValues(original);

            // For any members that don't have RoundtripOriginal applied, EF can't determine modification
            // state by doing value comparisons. To avoid losing updates in these cases, we must explicitly
            // mark such members as modified.
            Type entityType = current.GetType();
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
            AttributeCollection          attributes = TypeDescriptor.GetAttributes(entityType);
            bool isRoundtripType = attributes[typeof(RoundtripOriginalAttribute)] != null;

            foreach (var fieldMetadata in stateEntry.CurrentValues.DataRecordInfo.FieldMetadata)
            {
                string             memberName = stateEntry.CurrentValues.GetName(fieldMetadata.Ordinal);
                PropertyDescriptor property   = properties[memberName];
                if (property != null &&
                    (property.Attributes[typeof(RoundtripOriginalAttribute)] == null && !isRoundtripType) &&
                    property.Attributes[typeof(ExcludeAttribute)] == null)
                {
                    stateEntry.SetModifiedProperty(memberName);
                }
            }
            return(stateEntry);
        }
Beispiel #3
0
        private static void UpdateOriginalValues(ObjectStateEntry entry, EntityInfo entityInfo)
        {
            var originalValuesMap = entityInfo.OriginalValuesMap;

            if (originalValuesMap == null || originalValuesMap.Keys.Count == 0)
            {
                return;
            }

            var originalValuesRecord = entry.GetUpdatableOriginalValues();

            originalValuesMap.ToList().ForEach(kvp => {
                var propertyName  = kvp.Key;
                var originalValue = kvp.Value;

                try {
                    entry.SetModifiedProperty(propertyName);
                    if (originalValue is JObject)
                    {
                        // only really need to perform updating original values on key properties
                        // and a complex object cannot be a key.
                    }
                    else
                    {
                        var ordinal   = originalValuesRecord.GetOrdinal(propertyName);
                        var fieldType = originalValuesRecord.GetFieldType(ordinal);
                        var originalValueConverted = ConvertValue(originalValue, fieldType);

                        if (originalValueConverted == null)
                        {
                            // bug - hack because of bug in EF - see
                            // http://social.msdn.microsoft.com/Forums/nl/adodotnetentityframework/thread/cba1c425-bf82-4182-8dfb-f8da0572e5da
                            var temp = entry.CurrentValues[ordinal];
                            entry.CurrentValues.SetDBNull(ordinal);
                            entry.ApplyOriginalValues(entry.Entity);
                            entry.CurrentValues.SetValue(ordinal, temp);
                        }
                        else
                        {
                            originalValuesRecord.SetValue(ordinal, originalValueConverted);
                        }
                    }
                } catch (Exception e) {
                    if (e.Message.Contains(" part of the entity's key"))
                    {
                        throw;
                    }
                    else
                    {
                        // this can happen for "custom" data entity properties.
                    }
                }
            });
        }
Beispiel #4
0
        public static ObjectStateEntry AttachAsModifiedInternal <T>(T current, T original, ObjectContext objectContext)
        {
            ObjectStateEntry objectStateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(current);

            objectStateEntry.ApplyOriginalValues(original);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            AttributeCollection          attributes = TypeDescriptor.GetAttributes(typeof(T));

            //bool flag = attributes[typeof(RoundtripOriginalAttribute)] != null;
            foreach (FieldMetadata current2 in objectStateEntry.CurrentValues.DataRecordInfo.FieldMetadata)
            {
                string             name = objectStateEntry.CurrentValues.GetName(current2.Ordinal);
                PropertyDescriptor propertyDescriptor = properties[name];
                if (propertyDescriptor != null /* && propertyDescriptor.Attributes[typeof(RoundtripOriginalAttribute)] == null &&
                                                * !flag && propertyDescriptor.Attributes[typeof(ExcludeAttribute)] == null*/)
                {
                    objectStateEntry.SetModifiedProperty(name);
                }
            }
            return(objectStateEntry);
        }