示例#1
0
        /// <summary>Creates a many-to-one or one-to-one relationship between two objects in the object context.</summary>
        /// <param name="entity">The object being attached.</param>
        /// <exception cref="T:System.ArgumentNullException">When the  entity  is null.</exception>
        /// <exception cref="T:System.InvalidOperationException">When the  entity  cannot be related to the current related end. This can occur when the association in the conceptual schema does not support a relationship between the two types.</exception>
        public void Attach(TEntity entity)
        {
            Check.NotNull(entity, "entity");

            CheckOwnerNull();
            Attach(new[] { EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext) }, false);
        }
示例#2
0
        /// <summary>Defines relationships between an object and a collection of related objects in an object context.</summary>
        /// <remarks>
        /// Loads related entities into the local collection. If the collection is already filled
        /// or partially filled, merges existing entities with the given entities. The given
        /// entities are not assumed to be the complete set of related entities.
        /// Owner and all entities passed in must be in Unchanged or Modified state. We allow
        /// deleted elements only when the state manager is already tracking the relationship
        /// instance.
        /// </remarks>
        /// <param name="entities">Collection of objects in the object context that are related to the source object.</param>
        /// <exception cref="T:System.ArgumentNullException"> entities  collection is null.</exception>
        /// <exception cref="T:System.InvalidOperationException">
        /// The source object or an object in the  entities  collection is null or is not in an
        /// <see
        ///     cref="F:System.Data.Entity.EntityState.Unchanged" />
        /// or <see cref="F:System.Data.Entity.EntityState.Modified" /> state.-or-The relationship cannot be defined based on the EDM metadata. This can occur when the association in the conceptual schema does not support a relationship between the two types.
        /// </exception>
        public void Attach(IEnumerable <TEntity> entities)
        {
            Check.NotNull(entities, "entities");
            CheckOwnerNull();
            IList <IEntityWrapper> wrappedEntities = new List <IEntityWrapper>();

            foreach (var entity in entities)
            {
                wrappedEntities.Add(EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext));
            }
            Attach(wrappedEntities, true);
        }
示例#3
0
 public void OnCollectionDeserialized(StreamingContext context)
 {
     if (_relatedEntities != null)
     {
         // We need to call this here so that the hash set will be fully constructed
         // ready for access.  Normally, this would happen later in the process.
         _relatedEntities.OnDeserialization(null);
         _wrappedRelatedEntities = new Dictionary <TEntity, IEntityWrapper>();
         foreach (TEntity entity in _relatedEntities)
         {
             _wrappedRelatedEntities.Add(entity, EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext));
         }
     }
 }
示例#4
0
 internal override void Include(bool addRelationshipAsUnchanged, bool doAttach)
 {
     if (null != _wrappedRelatedEntities && null != this.ObjectContext)
     {
         List <IEntityWrapper> wrappedRelatedEntities = new List <IEntityWrapper>(_wrappedRelatedEntities.Values);
         foreach (IEntityWrapper wrappedEntity in wrappedRelatedEntities)
         {
             // Sometimes with mixed POCO and IPOCO, you can get different instances of IEntityWrappers stored in the IPOCO related ends
             // These should be replaced by the IEntityWrapper that is stored in the context
             IEntityWrapper identityWrapper = EntityWrapperFactory.WrapEntityUsingContext(wrappedEntity.Entity, WrappedOwner.Context);
             if (identityWrapper != wrappedEntity)
             {
                 _wrappedRelatedEntities[(TEntity)identityWrapper.Entity] = identityWrapper;
             }
             IncludeEntity(identityWrapper, addRelationshipAsUnchanged, doAttach);
         }
     }
 }
        internal override void Include(bool addRelationshipAsUnchanged, bool doAttach)
        {
            Debug.Assert(this.ObjectContext != null, "Should not be trying to add entities to state manager if context is null");

            // If we have an actual value or a key for this reference, add it to the context
            if (null != _wrappedCachedValue.Entity)
            {
                // Sometimes with mixed POCO and IPOCO, you can get different instances of IEntityWrappers stored in the IPOCO related ends
                // These should be replaced by the IEntityWrapper that is stored in the context
                IEntityWrapper identityWrapper = EntityWrapperFactory.WrapEntityUsingContext(_wrappedCachedValue.Entity, WrappedOwner.Context);
                if (identityWrapper != _wrappedCachedValue)
                {
                    _wrappedCachedValue = identityWrapper;
                }
                IncludeEntity(_wrappedCachedValue, addRelationshipAsUnchanged, doAttach);
            }
            else if (DetachedEntityKey != null)
            {
                IncludeEntityKey(doAttach);
            }
            // else there is nothing to add for this relationship
        }
 public void OnRefDeserialized(StreamingContext context)
 {
     _wrappedCachedValue = EntityWrapperFactory.WrapEntityUsingContext(_cachedValue, ObjectContext);
 }
 /// <summary>
 /// Attaches an entity to the EntityReference. The given
 /// entity is not assumed to be the complete set of related entities.
 ///
 /// Owner and all entities passed in must be in Unchanged or Modified state.
 /// Deleted elements are allowed only when the state manager is already tracking the relationship
 /// instance.
 /// </summary>
 /// <param name="entity">The entity to attach to the EntityCollection</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="entity"/> is null.</exception>
 /// <exception cref="InvalidOperationException">Thrown when the entity cannot be related via the current relationship end.</exception>
 public void Attach(TEntity entity)
 {
     CheckOwnerNull();
     EntityUtil.CheckArgumentNull(entity, "entity");
     Attach(new IEntityWrapper[] { EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext) }, false);
 }
示例#8
0
 internal bool RemoveInternal(TEntity entity)
 {
     return(Remove(EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext), /*preserveForeignKey*/ false));
 }
示例#9
0
        /// <summary>Adds an object to the collection.</summary>
        /// <param name="item">
        /// An object to add to the collection.  entity  must implement
        /// <see
        ///     cref="T:System.Data.Entity.Core.Objects.DataClasses.IEntityWithRelationships" />
        /// .
        /// </param>
        /// <exception cref="T:System.ArgumentNullException"> entity  is null.</exception>
        public void Add(TEntity item)
        {
            Check.NotNull(item, "item");

            Add(EntityWrapperFactory.WrapEntityUsingContext(item, ObjectContext));
        }
示例#10
0
 /// <summary>
 ///
 /// </summary>
 public void Add(TEntity entity)
 {
     EntityUtil.CheckArgumentNull(entity, "entity");
     Add(EntityWrapperFactory.WrapEntityUsingContext(entity, ObjectContext));
 }