Exemplo n.º 1
0
        private object AddElement <T>(IChangeTracker changeTracker, IEntityManager entityManager, T existing, object updateItem, object dbCollection)
        {
            if (!_isOwned)
            {
                updateItem = changeTracker.AttachAndReloadAssociatedEntity(updateItem);
            }
            else if (changeTracker.GetItemState(updateItem) == EntityState.Detached)
            {
                var instance = entityManager.CreateEmptyEntityWithKey(updateItem);

                changeTracker.AddItem(instance);
                changeTracker.UpdateItem(updateItem, instance);

                foreach (var childMember in Members)
                {
                    childMember.Update(changeTracker, entityManager, instance, updateItem);
                }

                updateItem = instance;
            }

            dbCollection.GetType().GetMethod("Add").Invoke(dbCollection, new[] { updateItem });

            if (_isOwned)
            {
                changeTracker.AttachCyclicNavigationProperty(existing, updateItem, GetMappedNaviationProperties());
            }

            return(updateItem);
        }
Exemplo n.º 2
0
        public object AttachAndReloadAssociatedEntity(object entity)
        {
            var localCopy = FindTrackedEntity(entity);

            if (localCopy != null)
            {
                return(localCopy);
            }

            if (_context.Entry(entity).State == EntityState.Detached)
            {
                // TODO look into a possible better way of doing this, I don't particularly like it
                // will add a key-only object to the change tracker. at the moment this is being reloaded,
                // performing a db query which would impact performance
                var entityType = ObjectContext.GetObjectType(entity.GetType());
                var instance   = _entityManager.CreateEmptyEntityWithKey(entity);

                _context.Set(entityType).Attach(instance);
                _context.Entry(instance).Reload();

                AttachRequiredNavigationProperties(entity, instance);
                return(instance);
            }

            if (GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached)
            {
                _context.Entry(entity).Reload();
            }

            return(entity);
        }