public override void CascadeStoreOperation(CustomStoreOperationKind changeKind)
        {
            CustomObjectContext context = ObjectContextWeakRef.Target;

            Debug.Assert(null != context);

            Queue <object> objectsToBeRemoved = null;

            foreach (object entityObject in this)
            {
                if (CustomStoreOperationKind.Add == changeKind)
                {
                    context.AddObject(RelationshipMetadata.To.EntitySetType.Name, entityObject);
                }
                else if (CustomStoreOperationKind.Delete == changeKind)
                {
                    if (null == objectsToBeRemoved)
                    {
                        objectsToBeRemoved = new Queue <object>();
                    }
                    objectsToBeRemoved.Enqueue(entityObject);
                }
            }

            if (null != objectsToBeRemoved && objectsToBeRemoved.Count > 0)
            {
                foreach (object entityObject in objectsToBeRemoved)
                {
                    Remove(entityObject);
                    DeleteObjectIfTransietOrphan(context, entityObject);
                }
            }
        }
        protected override void UpdateRelatedEndInternal(
            CustomObjectContext objectContext,
            RelatedEndEntityObject relatedEntityObject,
            CustomStoreOperationKind changeKind
            )
        {
            if (CustomStoreOperationKind.Add == changeKind)
            {
                if (null != RelationshipMetadata.To.RelatedProperty)
                {
                    RelationshipMetadata.To.RelatedProperty.SetValue(relatedEntityObject, _thisEndObject);
                }

                if (null != objectContext)
                {
                    objectContext.AddObject(RelationshipMetadata.To.EntitySetType.Name, relatedEntityObject);
                }
            }
            else
            {
                Debug.Assert(CustomStoreOperationKind.Delete == changeKind);

                if (null != RelationshipMetadata.To.RelatedProperty)
                {
                    RelationshipMetadata.To.RelatedProperty.SetValue(relatedEntityObject, null);
                }

                if (null != objectContext)
                {
                    objectContext.DeleteObject(relatedEntityObject);
                }
            }
        }
        protected override void UpdateRelatedEndInternal(
            CustomObjectContext objectContext,
            RelatedEndEntityObject relatedEntityObject,
            CustomStoreOperationKind changeKind
            )
        {
            if (CustomStoreOperationKind.Add == changeKind)
            {
                if (null != RelationshipMetadata.To.RelatedProperty)
                {
                    object relatedManyToManyCollection = RelationshipMetadata.To.RelatedProperty.GetValue(relatedEntityObject);
                    Debug.Assert(null != relatedManyToManyCollection);

                    if (relatedManyToManyCollection is CustomObjectCollectionBase)
                    {
                        Debug.Assert(relatedManyToManyCollection is CustomObjectCollectionBase);
                        var managedManyToManyCollection = (CustomObjectCollectionBase)relatedManyToManyCollection;
                        managedManyToManyCollection.Add(_thisEndObject);
                    }
                    else
                    {
                        MethodInfo addMethod = relatedManyToManyCollection.GetType().GetMethod("Add");
                        Debug.Assert(null != addMethod);
                        addMethod.Invoke(relatedManyToManyCollection, new object[] { _thisEndObject });
                    }
                }

                if (null != objectContext)
                {
                    objectContext.AddObject(RelationshipMetadata.To.EntitySetType.Name, relatedEntityObject);
                }
            }
            else
            {
                Debug.Assert(CustomStoreOperationKind.Delete == changeKind);

                if (null != RelationshipMetadata.To.RelatedProperty)
                {
                    object relatedManyToManyCollection = RelationshipMetadata.To.RelatedProperty.GetValue(relatedEntityObject);
                    if (relatedManyToManyCollection is CustomObjectCollectionBase)
                    {
                        Debug.Assert(relatedManyToManyCollection is CustomObjectCollectionBase);
                        var managedManyToManyCollection = (CustomObjectCollectionBase)relatedManyToManyCollection;
                        managedManyToManyCollection.Remove(_thisEndObject);
                    }
                    else
                    {
                        MethodInfo removeMethod = relatedManyToManyCollection.GetType().GetMethod("Remove");
                        Debug.Assert(null != removeMethod);
                        removeMethod.Invoke(relatedManyToManyCollection, new object[] { _thisEndObject });
                    }

                    if (null != objectContext)
                    {
                        DeleteObjectIfTransietOrphan(objectContext, relatedEntityObject);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public void PreAtachExistingDataObjects(CustomObjectContext objectContext)
 {
     if (!_defaultDataCreated)
     {
         CreateAndAttachDefaultDataObjects(objectContext);
         objectContext.SaveAllChanges();
         _defaultDataCreated = true;
     }
     else
     {
         foreach (var entitySetEntry in _entitySetMap.Values)
         {
             foreach (object entityObject in entitySetEntry.EntityObjects)
             {
                 objectContext.AddObject(entitySetEntry.EntitySetType.Name, entityObject);
             }
         }
         objectContext.AcceptAllChanges();
     }
 }
        public override void CascadeStoreOperation(CustomStoreOperationKind operationKind)
        {
            CustomObjectContext context = ObjectContextWeakRef.Target;

            Debug.Assert(null != context);

            foreach (object entityObject in this)
            {
                if (CustomStoreOperationKind.Add == operationKind)
                {
                    context.AddObject(RelationshipMetadata.To.EntitySetType.Name, entityObject);
                }
                else if (CustomStoreOperationKind.Delete == operationKind)
                {
                    context.DeleteObject(entityObject);
                }
                else
                {
                    Debug.Fail("Unexpected store operation kind", operationKind.ToString());
                }
            }
        }