/// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public void Add(T item)
        {
            var entity = AssertBrightstarObject(item, "item");

            if (_isInverse)
            {
                _context.AddArc(entity, _propertyTypeUri, _parent, false);
            }
            else
            {
                _context.AddArc(_parent, _propertyTypeUri, entity, false);
            }
            if (IsLoaded)
            {
                AddToLoadedObjects(entity);
            }
        }
        /// <summary>
        /// Invoked by the generated class to change the value of a property whose type
        /// is another entity
        /// </summary>
        /// <typeparam name="T">The type of the related entity</typeparam>
        /// <param name="propertyName">The name of the property that represents the relationship</param>
        /// <param name="value">The new related entity</param>
        /// <exception cref="EntityFrameworkException">Thrown if this object is not currently attached to a context</exception>
        public void SetRelatedObject <T>(string propertyName, T value) where T : class
        {
            if (!IsAttached)
            {
                if (_currentPropertyValues.ContainsKey(propertyName) &&
                    SafeEquals(_currentPropertyValues[propertyName], value))
                {
                    return;
                }
                _currentItemValues[propertyName] = value;
                OnPropertyChanged(propertyName);
                return;
            }

            var entity = ValidateAndAttach(value, propertyName);

            if (_currentPropertyValues.ContainsKey(propertyName) &&
                SafeEquals(_currentPropertyValues[propertyName], entity))
            {
                return;
            }

            var  propertyHint = GetPropertyHint(propertyName);
            bool isSingleValuedForwardProperty = false;

            // Remove existing arcs
            if (propertyHint.MappingType == PropertyMappingType.Arc)
            {
                var existingDataObject = DataObject.GetPropertyValue(propertyHint.SchemaTypeUri) as DataObject;
                if (existingDataObject != null)
                {
                    _context.RemoveArc(DataObject, propertyHint.SchemaTypeUri, existingDataObject);
                }
                // If the value entity has a single-value inverse properties of this type then all arcs needs removing
                var invArcProperties = _context.GetInverseArcProperties(typeof(T), propertyHint.SchemaTypeUri).ToList();
                if (invArcProperties.Any() && invArcProperties.All(x => !(IsCollectionType(x.PropertyType))))
                {
                    foreach (var existingValueRef in entity.DataObject.GetInverseOf(propertyHint.SchemaTypeUri).ToList())
                    {
                        _context.RemoveArc(existingValueRef, propertyHint.SchemaTypeUri, entity.DataObject);
                    }
                }

                // Clean up any inverse collection properties for this property
                foreach (var p in invArcProperties)
                {
                    if (existingDataObject != null)
                    {
                        foreach (var trackedObject in _context.GetTrackedObjects(existingDataObject))
                        {
                            var otherCollection = p.GetValue(trackedObject, null) as IBrightstarEntityCollection;
                            if (otherCollection != null)
                            {
                                otherCollection.RemoveFromLoadedObjects(DataObject.Identity);
                            }
                        }
                    }
                }
            }
            else if (propertyHint.MappingType == PropertyMappingType.InverseArc)
            {
                var props = _context.GetArcProperties(typeof(T), propertyHint.SchemaTypeUri).ToList();
                foreach (var existingDataObject in DataObject.GetInverseOf(propertyHint.SchemaTypeUri).ToList())
                {
                    _context.RemoveArc(existingDataObject, propertyHint.SchemaTypeUri, DataObject);
                    // Clean up any forward collection properties for this property
                    foreach (var trackedObject in _context.GetTrackedObjects(existingDataObject))
                    {
                        foreach (var p in props)
                        {
                            var collection = p.GetValue(trackedObject, null) as IBrightstarEntityCollection;
                            if (collection != null)
                            {
                                collection.RemoveFromLoadedObjects(this.DataObject.Identity);
                            }
                        }
                    }
                }
                // If the value entity has a single-value forward property of this type then all existing arcs from that value need removing
                if (props.Any() && props.All(x => !(IsCollectionType(x.PropertyType))))
                {
                    isSingleValuedForwardProperty = true;
                    foreach (var existingValueRef in entity.DataObject.GetPropertyValues(propertyHint.SchemaTypeUri).OfType <IDataObject>().ToList())
                    {
                        _context.RemoveArc(entity.DataObject, propertyHint.SchemaTypeUri, existingValueRef);
                    }
                }
            }

            if (entity == null)
            {
                // No new value so just record a null for this property and return
                _currentPropertyValues[propertyName] = null;
                OnPropertyChanged(propertyName);
                return;
            }

            // Create new arc
            if (propertyHint.MappingType == PropertyMappingType.Arc)
            {
                _context.AddArc(this, propertyHint.SchemaTypeUri, entity, true);
            }
            else if (propertyHint.MappingType == PropertyMappingType.InverseArc)
            {
                _context.AddArc(entity, propertyHint.SchemaTypeUri, this, isSingleValuedForwardProperty);
            }

            // Update cache
            _currentPropertyValues[propertyName] = entity;

            OnPropertyChanged(propertyName);
        }