示例#1
0
 internal override void ClearCollectionOrRef(IEntityWrapper wrappedEntity, RelationshipNavigation navigation, bool doCascadeDelete)
 {
     if (null != _wrappedRelatedEntities)
     {
         //copy into list because changing collection member is not allowed during enumeration.
         // If possible avoid copying into list.
         var tempCopy = new List <IEntityWrapper>(_wrappedRelatedEntities.Values);
         foreach (var wrappedCurrent in tempCopy)
         {
             // Following condition checks if we have already visited this graph node. If its true then
             // we should not do fixup because that would cause circular loop
             if ((wrappedEntity.Entity == wrappedCurrent.Entity) &&
                 (navigation.Equals(RelationshipNavigation)))
             {
                 Remove(
                     wrappedCurrent, /*fixup*/ false, /*deleteEntity*/ false, /*deleteOwner*/ false, /*applyReferentialConstraints*/
                     false, /*preserveForeignKey*/ false);
             }
             else
             {
                 Remove(
                     wrappedCurrent, /*fixup*/ true, doCascadeDelete, /*deleteOwner*/ false, /*applyReferentialConstraints*/ false,
                     /*preserveForeignKey*/ false);
             }
         }
         Debug.Assert(
             _wrappedRelatedEntities.Count == 0, "After removing all related entities local collection count should be zero");
     }
 }
示例#2
0
 internal EntityReference(
     IEntityWrapper wrappedOwner,
     RelationshipNavigation navigation,
     IRelationshipFixer relationshipFixer)
     : base(wrappedOwner, navigation, relationshipFixer)
 {
 }
 internal override void ClearCollectionOrRef(
     IEntityWrapper wrappedEntity,
     RelationshipNavigation navigation,
     bool doCascadeDelete)
 {
     if (wrappedEntity == null)
     {
         wrappedEntity = NullEntityWrapper.NullWrapper;
     }
     if (this._wrappedCachedValue.Entity != null)
     {
         if (wrappedEntity.Entity == this._wrappedCachedValue.Entity && navigation.Equals((object)this.RelationshipNavigation))
         {
             this.Remove(this._wrappedCachedValue, false, false, false, false, false);
         }
         else
         {
             this.Remove(this._wrappedCachedValue, true, doCascadeDelete, false, true, false);
         }
     }
     else if (this.WrappedOwner.Entity != null && this.WrappedOwner.Context != null && !this.UsingNoTracking)
     {
         this.WrappedOwner.Context.ObjectStateManager.GetEntityEntry(this.WrappedOwner.Entity).DeleteRelationshipsThatReferenceKeys(this.RelationshipSet, this.ToEndMember);
     }
     if (this.WrappedOwner.Entity == null)
     {
         return;
     }
     this.DetachedEntityKey = (EntityKey)null;
 }
 internal EntityCollection(
     IEntityWrapper wrappedOwner,
     RelationshipNavigation navigation,
     IRelationshipFixer relationshipFixer)
     : base(wrappedOwner, navigation, relationshipFixer)
 {
 }
示例#5
0
        public static Mock <EntityReference <TEntity> > CreateMockEntityReference <TEntity>(TEntity refreshedValue)
            where TEntity : class
        {
            var relationshipNavigation = new RelationshipNavigation(
                relationshipName: "relationship",
                from: "from",
                to: "to",
                fromAccessor: new NavigationPropertyAccessor(string.Empty),
                toAccessor: new NavigationPropertyAccessor(string.Empty));

            var entityReferenceMock = new Mock <EntityReference <TEntity> >(
                Internal.MockHelper.CreateMockEntityWrapper().Object,
                relationshipNavigation,
                new Mock <IRelationshipFixer>(MockBehavior.Strict).Object)
            {
                CallBase = true
            };

            var associationType = new AssociationType(
                name: "associationName",
                namespaceName: "associationNamespace",
                foreignKey: true,
                dataSpace: DataSpace.CSpace);

            entityReferenceMock.Setup(m => m.RelationMetadata).Returns(associationType);

            var associationSet = new AssociationSet(name: "associationSetName", associationType: associationType);

            entityReferenceMock.Setup(m => m.RelationshipSet).Returns(associationSet);

            entityReferenceMock.Setup(m => m.ObjectContext).Returns(Objects.MockHelper.CreateMockObjectContext <DbDataRecord>());

            var objectQueryMock = Objects.MockHelper.CreateMockObjectQuery(refreshedValue);

            var hasResults = refreshedValue != null;

            entityReferenceMock.Setup(m => m.ValidateLoad <TEntity>(It.IsAny <MergeOption>(), It.IsAny <string>(), out hasResults))
            .Returns(() => objectQueryMock.Object);

            var refType =
                new RefType(new EntityType(name: "entityTypeName", namespaceName: "entityTypeNamespace", dataSpace: DataSpace.CSpace));

            var fromEndMember = new AssociationEndMember(
                name: "fromEndMember",
                endRefType: refType,
                multiplicity: RelationshipMultiplicity.Many);

            entityReferenceMock.Setup(m => m.FromEndMember).Returns(fromEndMember);

            var toEndMember = new AssociationEndMember(
                name: "toEndMember",
                endRefType: refType,
                multiplicity: RelationshipMultiplicity.Many);

            entityReferenceMock.Setup(m => m.ToEndMember).Returns(toEndMember);

            entityReferenceMock.Setup(m => m.GetInternalEnumerable()).Returns(new[] { refreshedValue });

            return(entityReferenceMock);
        }
 internal EntityReference(
     IEntityWrapper wrappedOwner,
     RelationshipNavigation navigation,
     IRelationshipFixer relationshipFixer)
     : base(wrappedOwner, navigation, relationshipFixer)
 {
     this._wrappedCachedValue = NullEntityWrapper.NullWrapper;
 }
        public override bool Equals(object obj)
        {
            RelationshipNavigation relationshipNavigation = obj as RelationshipNavigation;

            if (this == relationshipNavigation)
            {
                return(true);
            }
            if (this != null && relationshipNavigation != null && (this.RelationshipName == relationshipNavigation.RelationshipName && this.From == relationshipNavigation.From))
            {
                return(this.To == relationshipNavigation.To);
            }
            return(false);
        }
示例#8
0
        internal override void ClearCollectionOrRef(IEntityWrapper wrappedEntity, RelationshipNavigation navigation, bool doCascadeDelete)
        {
            if (wrappedEntity == null)
            {
                wrappedEntity = NullEntityWrapper.NullWrapper;
            }
            if (null != _wrappedCachedValue.Entity)
            {
                // Following condition checks if we have already visited this graph node. If its true then
                // we should not do fixup because that would cause circular loop
                if ((wrappedEntity.Entity == _wrappedCachedValue.Entity) &&
                    (navigation.Equals(RelationshipNavigation)))
                {
                    Remove(
                        _wrappedCachedValue, /*fixup*/ false, /*deleteEntity*/ false, /*deleteOwner*/ false, /*applyReferentialConstraints*/
                        false, /*preserveForeignKey*/ false);
                }
                else
                {
                    Remove(
                        _wrappedCachedValue, /*fixup*/ true, doCascadeDelete, /*deleteOwner*/ false, /*applyReferentialConstraints*/ true,
                        /*preserveForeignKey*/ false);
                }
            }
            else
            {
                // this entity reference could be replacing a relationship that points to a key entry
                // we need to search relationships on the Owner entity to see if this is true, and if so remove the relationship entry
                if (WrappedOwner.Entity != null &&
                    WrappedOwner.Context != null &&
                    !UsingNoTracking)
                {
                    var ownerEntry = WrappedOwner.Context.ObjectStateManager.GetEntityEntry(WrappedOwner.Entity);
                    ownerEntry.DeleteRelationshipsThatReferenceKeys(RelationshipSet, ToEndMember);
                }
            }

            // If we have an Owner, clear the DetachedEntityKey.
            // If we do not have an owner, retain the key so that we can resolve the difference when the entity is attached to a context
            if (WrappedOwner.Entity != null)
            {
                // Clear the detachedEntityKey as well. In cases where we have to fix up the detachedEntityKey, we will not always be able to detect
                // if we have *only* a Deleted relationship for a given entity/relationship/role, so clearing this here will ensure that
                // even if no other relationships are added, the key value will still be correct.
                DetachedEntityKey = null;
            }
        }
 internal override void ClearCollectionOrRef(
     IEntityWrapper wrappedEntity,
     RelationshipNavigation navigation,
     bool doCascadeDelete)
 {
     if (this._wrappedRelatedEntities == null)
     {
         return;
     }
     foreach (IEntityWrapper wrappedEntity1 in new List <IEntityWrapper>((IEnumerable <IEntityWrapper>) this._wrappedRelatedEntities.Values))
     {
         if (wrappedEntity.Entity == wrappedEntity1.Entity && navigation.Equals((object)this.RelationshipNavigation))
         {
             this.Remove(wrappedEntity1, false, false, false, false, false);
         }
         else
         {
             this.Remove(wrappedEntity1, true, doCascadeDelete, false, false, false);
         }
     }
 }
 internal EntityReference(IEntityWrapper wrappedOwner, RelationshipNavigation navigation, IRelationshipFixer relationshipFixer)
     : base(wrappedOwner, navigation, relationshipFixer)
 {
 }
示例#11
0
 // <summary>
 // Used during relationship fixup when the source end of the relationship is not
 // yet in the relationships list, and needs to be created
 // </summary>
 // <param name="navigation"> RelationshipNavigation to be set on new RelatedEnd </param>
 // <param name="relationshipManager"> RelationshipManager to use for creating the new end </param>
 // <returns> Reference to the new collection or reference on the other end of the relationship </returns>
 RelatedEnd IRelationshipFixer.CreateSourceEnd(RelationshipNavigation navigation, RelationshipManager relationshipManager)
 {
     return(relationshipManager.CreateRelatedEnd <TTargetEntity, TSourceEntity>(
                navigation, _targetRoleMultiplicity, _sourceRoleMultiplicity, /*existingRelatedEnd*/ null));
 }
示例#12
0
 RelatedEnd IRelationshipFixer.CreateSourceEnd(
     RelationshipNavigation navigation,
     RelationshipManager relationshipManager)
 {
     return(relationshipManager.CreateRelatedEnd <TTargetEntity, TSourceEntity>(navigation, this._targetRoleMultiplicity, this._sourceRoleMultiplicity, (RelatedEnd)null));
 }