internal void ForceCollectionPropertyUpdates()
 {
     // Force save of complete collection for all literal collection properties
     foreach (var entry in _currentItemValues)
     {
         if (IsLiteralsCollection(entry.Value.GetType()))
         {
             var propertyUri = GetPropertyUri(entry.Key);
             var values      = DataObject.GetPropertyValues(propertyUri).OfType <object>().ToList();
             DataObject.RemovePropertiesOfType(propertyUri);
             foreach (var o in values)
             {
                 DataObject.AddProperty(propertyUri, o);
             }
         }
     }
     foreach (var entry in _currentPropertyCollections)
     {
         var properyHint = GetPropertyHint(entry.Key);
         if (properyHint != null && properyHint.MappingType == PropertyMappingType.Arc)
         {
             var values = DataObject.GetPropertyValues(properyHint.SchemaTypeUri).OfType <DataObject>().ToList();
             DataObject.RemovePropertiesOfType(properyHint.SchemaTypeUri);
             foreach (var o in values)
             {
                 DataObject.AddProperty(properyHint.SchemaTypeUri, o);
             }
         }
     }
 }
        /// <summary>
        /// Sets the collection of related entities for a specific property
        /// </summary>
        /// <typeparam name="T">The related entity type</typeparam>
        /// <param name="propertyName">The name of the property to be updated</param>
        /// <param name="relatedObjects">The new collection of related entities</param>
        public void SetRelatedObjects <T>(string propertyName, ICollection <T> relatedObjects) where T : class
        {
            if (!IsAttached)
            {
                var current = GetRelatedObjects <T>(propertyName);
                current.Set(relatedObjects);
                return;
            }

            if (_currentPropertyCollections.ContainsKey(propertyName))
            {
                var currentCollection = _currentPropertyCollections[propertyName] as IEntityCollection <T>;
                currentCollection.Set(relatedObjects);
            }
            else
            {
                var propertyHint = GetPropertyHint(propertyName);
                if (propertyHint.MappingType == PropertyMappingType.Arc)
                {
                    DataObject.RemovePropertiesOfType(propertyHint.SchemaTypeUri);
                    if (relatedObjects.Any(r => !(r is BrightstarEntityObject)))
                    {
                        throw new ArgumentException("Related objects must all extend BrightstarDB.EntityFramework.BrightstarEntityObject");
                    }
                    foreach (var i in relatedObjects.Cast <BrightstarEntityObject>())
                    {
                        if (!i.IsAttached)
                        {
                            i.Attach(_context);
                        }
                        DataObject.AddProperty(propertyHint.SchemaTypeUri, i.DataObject);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Attaches the object to the specified context
        /// </summary>
        /// <param name="context"></param>
        /// <param name="overwriteExisting"></param>
        public void Attach(EntityContext context, bool overwriteExisting = false)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!(context is BrightstarEntityContext))
            {
                throw new ArgumentException(
                          String.Format("An object of type {0} can only be attached to a context that extends {1}",
                                        GetType().FullName, typeof(BrightstarEntityContext).FullName));
            }
            if (IsAttached)
            {
                if (!context.Equals(_context))
                {
                    _context.UntrackObject(this);
                }
            }
            _context = context as BrightstarEntityContext;
            if (_identity == null)
            {
                AssertIdentity();
            }
            if (DataObject == null && _identity != null)
            {
                DataObject = _context.GetDataObject(new Uri(_identity), false);
                var identityInfo = EntityMappingStore.GetIdentityInfo(GetType());
                if (identityInfo != null && identityInfo.EnforceClassUniqueConstraint && !overwriteExisting)
                {
                    _context.EnforceClassUniqueConstraint(_identity, EntityMappingStore.MapTypeToUris(GetType()));
                }
                foreach (var typeUri in EntityMappingStore.MapTypeToUris(GetType()))
                {
                    if (!String.IsNullOrEmpty(typeUri))
                    {
                        var typeDo = _context.GetDataObject(new Uri(typeUri), false);
                        if (typeDo != null)
                        {
                            DataObject.AddProperty(Client.DataObject.TypeDataObject, typeDo);
                        }
                    }
                }
            }
//            if (DataObject != null)
//            {
            _context.TrackObject(this);
//            }

            if (_currentItemValues != null)
            {
                foreach (var propertyName in _currentItemValues.Keys.ToList())
                {
                    PropertyInfo p = GetType().GetProperty(propertyName);
                    p.SetValue(this, _currentItemValues[propertyName], null);
                }
                _currentItemValues.Clear();
            }
        }
        /// <summary>
        /// Attaches the object to the specified context
        /// </summary>
        /// <param name="context"></param>
        public void Attach(EntityContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (!(context is BrightstarEntityContext))
            {
                throw new ArgumentException(
                          String.Format("An object of type {0} can only be attached to a context that extends {1}",
                                        GetType().FullName, typeof(BrightstarEntityContext).FullName));
            }
            if (IsAttached)
            {
                if (!context.Equals(_context))
                {
                    _context.UntrackObject(this);
                }
            }
            _context = context as BrightstarEntityContext;
            if (DataObject == null && _identity != null)
            {
                DataObject = _context.GetDataObject(new Uri(_identity), false);
                foreach (var typeUri in _context.Mappings.MapTypeToUris(GetType()))
                {
                    if (!String.IsNullOrEmpty(typeUri))
                    {
                        var typeDo = _context.GetDataObject(new Uri(typeUri), false);
                        if (typeDo != null)
                        {
                            DataObject.AddProperty(Client.DataObject.TypeDataObject, typeDo);
                        }
                    }
                }
            }
            if (DataObject != null)
            {
                _context.TrackObject(this);
            }

            if (_currentItemValues != null)
            {
                foreach (var propertyName in _currentItemValues.Keys.ToList())
                {
                    PropertyInfo p = GetType().GetProperty(propertyName);
                    p.SetValue(this, _currentItemValues[propertyName], null);
                }
                _currentItemValues.Clear();
            }
        }