protected override void SetRecordValue(int ordinal, object value)
        {
            StateManagerMemberMetadata member = _metadata.Member(ordinal);

            // We do not allow setting complex properties through writeable original values.
            // Instead individual scalar properties can be set on a data record that represents the complex type.
            if (member.IsComplex)
            {
                throw EntityUtil.SetOriginalComplexProperties(member.CLayerName);
            }

            // Null values are represented in data records as DBNull.Value, so translate appropriately
            object fieldValue = value ?? DBNull.Value;

            EntityEntry entry    = _cacheEntry as EntityEntry;
            EntityState oldState = entry.State;

            // Only update the original values if the new value is different from the value currently set on the entity
            if (entry.HasRecordValueChanged(this, ordinal, fieldValue))
            {
                // Since the original value is going to be set, validate that is doesn't violate any restrictions

                // Throw if trying to change the original value of the primary key
                if (member.IsPartOfKey)
                {
                    throw EntityUtil.SetOriginalPrimaryKey(member.CLayerName);
                }

                // Verify non-nullable EDM members are not being set to null
                // Need to continue allowing CLR reference types to be set to null for backwards compatibility
                Type memberClrType = member.ClrType;
                if ((object)DBNull.Value == fieldValue &&
                    memberClrType.IsValueType &&
                    !member.CdmMetadata.Nullable)
                {
                    // Throw if the underlying CLR type of this property is not nullable, and it is being set to null
                    throw EntityUtil.NullOriginalValueForNonNullableProperty(member.CLayerName, member.ClrMetadata.Name, member.ClrMetadata.DeclaringType.FullName);
                }

                base.SetRecordValue(ordinal, value);

                // Update the state of the ObjectStateEntry if it has been marked as Modified
                if (oldState == EntityState.Unchanged && entry.State == EntityState.Modified)
                {
                    entry.ObjectStateManager.ChangeState(entry, oldState, EntityState.Modified);
                }

                // Set the individual property to modified
                entry.SetModifiedPropertyInternal(GetPropertyIndex(ordinal));
            }
        }
Exemplo n.º 2
0
        internal StateManagerTypeMetadata(EdmType edmType, ObjectTypeMapping mapping)
        {
            Debug.Assert(null != edmType, "null EdmType");
            Debug.Assert(Helper.IsEntityType(edmType) ||
                         Helper.IsComplexType(edmType),
                         "not Complex or EntityType");
            Debug.Assert(Object.ReferenceEquals(mapping, null) ||
                         Object.ReferenceEquals(mapping.EdmType, edmType),
                         "different EdmType instance");

            _typeUsage   = TypeUsage.Create(edmType);
            _recordInfo  = new DataRecordInfo(_typeUsage);
            _ocObjectMap = mapping;

            ReadOnlyMetadataCollection <EdmProperty> members = TypeHelpers.GetProperties(edmType);

            _members             = new StateManagerMemberMetadata[members.Count];
            _objectNameToOrdinal = new Dictionary <string, int>(members.Count);
            _cLayerNameToOrdinal = new Dictionary <string, int>(members.Count);

            ReadOnlyMetadataCollection <EdmMember> keyMembers = null;

            if (Helper.IsEntityType(edmType))
            {
                keyMembers = ((EntityType)edmType).KeyMembers;
            }

            for (int i = 0; i < _members.Length; ++i)
            {
                EdmProperty member = members[i];

                ObjectPropertyMapping memberMap = null;
                if (null != mapping)
                {
                    memberMap = mapping.GetPropertyMap(member.Name);
                    if (null != memberMap)
                    {
                        _objectNameToOrdinal.Add(memberMap.ClrProperty.Name, i); // olayer name
                    }
                }
                _cLayerNameToOrdinal.Add(member.Name, i); // clayer name

                // Determine whether this member is part of the identity of the entity.
                _members[i] = new StateManagerMemberMetadata(memberMap, member, ((null != keyMembers) && keyMembers.Contains(member)));
            }
        }
        internal StateManagerTypeMetadata(EdmType edmType, ObjectTypeMapping mapping)
        {
            Debug.Assert(null != edmType, "null EdmType");
            Debug.Assert(
                Helper.IsEntityType(edmType) ||
                Helper.IsComplexType(edmType),
                "not Complex or EntityType");
            Debug.Assert(
                ReferenceEquals(mapping, null) ||
                ReferenceEquals(mapping.EdmType, edmType),
                "different EdmType instance");

            _typeUsage = TypeUsage.Create(edmType);
            _recordInfo = new DataRecordInfo(_typeUsage);

            var members = TypeHelpers.GetProperties(edmType);
            _members = new StateManagerMemberMetadata[members.Count];
            _objectNameToOrdinal = new Dictionary<string, int>(members.Count);
            _cLayerNameToOrdinal = new Dictionary<string, int>(members.Count);

            ReadOnlyMetadataCollection<EdmMember> keyMembers = null;
            if (Helper.IsEntityType(edmType))
            {
                keyMembers = ((EntityType)edmType).KeyMembers;
            }

            for (var i = 0; i < _members.Length; ++i)
            {
                var member = members[i];

                ObjectPropertyMapping memberMap = null;
                if (null != mapping)
                {
                    memberMap = mapping.GetPropertyMap(member.Name);
                    if (null != memberMap)
                    {
                        _objectNameToOrdinal.Add(memberMap.ClrProperty.Name, i); // olayer name
                    }
                }
                _cLayerNameToOrdinal.Add(member.Name, i); // clayer name

                // Determine whether this member is part of the identity of the entity.
                _members[i] = new StateManagerMemberMetadata(memberMap, member, ((null != keyMembers) && keyMembers.Contains(member)));
            }
        }
Exemplo n.º 4
0
 internal StateManagerValue(StateManagerMemberMetadata metadata, object instance, object value)
 {
     memberMetadata = metadata;
     userObject = instance;
     originalValue = value;
 }
Exemplo n.º 5
0
 internal StateManagerValue(StateManagerMemberMetadata metadata, object instance, object value)
 {
     memberMetadata = metadata;
     userObject     = instance;
     originalValue  = value;
 }