示例#1
0
        /// <summary>
        /// Determines whether [is attached to] [the specified entity].
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">The entity.</param>
        /// <returns>
        ///   <c>true</c> if [is attached to] [the specified entity]; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">entity</exception>
        public bool IsAttachedTo <T>(T entity) where T : class
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            ObjectStateEntry entry;

            if (ObjectStateManager.TryGetObjectStateEntry(entity, out entry))
            {
                return(entry.State != EntityState.Detached);
            }
            return(false);
        }
示例#2
0
        public void Attach(object entity)
        {
            bool attached = false;
            ObjectStateManager stateManager = (this.context as IObjectContextAdapter).ObjectContext.ObjectStateManager;
            ObjectStateEntry   stateEntry   = null;

            if (stateManager.TryGetObjectStateEntry(entity, out stateEntry))
            {
                attached = stateEntry.State != System.Data.Entity.EntityState.Detached;
            }

            if (!attached)
            {
                this.context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            }
        }
示例#3
0
        public void Attach <TEntity>(TEntity entity)
            where TEntity : EntityObject
        {
            if (entity.EntityState != EntityState.Detached)
            {
                return;
            }
            // Let's see if the same entity with same key values are already there
            ObjectStateEntry entry;

            if (ObjectStateManager.TryGetObjectStateEntry(entity, out entry))
            {
            }
            else
            {
                _AttachMethodCache[typeof(TEntity).Name](this, entity);
            }
        }
示例#4
0
        /// <summary>
        /// Gets the state of the entity.
        /// </summary>
        /// <param name="manager">The manager.</param>
        /// <param name="entity">The entity.</param>
        /// <remarks>
        /// For more detail, see “RIA Services and Entity Framework POCOs”
        /// [http://thedatafarm.com/blog/data-access/ria-services-and-entity-framework-pocos/]
        /// by Julie Lerman
        /// </remarks>
        public static EntityState GetEntityState(ObjectStateManager manager, object entity)
        {
            if (manager == null)
            {
                return(default(EntityState));
            }

            ObjectStateEntry entry;

            if (manager.TryGetObjectStateEntry(entity, out entry))
            {
                return(entry.State);
            }
            else
            {
                return(EntityState.Detached);
            }
        }