コード例 #1
0
        public static ObjectContext GetObjectContext(this IEntityWithRelationships entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var relationshipManager = entity.RelationshipManager;

            var relatedEnd = relationshipManager.GetAllRelatedEnds()
                             .FirstOrDefault();

            if (relatedEnd == null)
            {
                throw new Exception("No relationships found");
            }

            var query = relatedEnd.CreateSourceQuery() as ObjectQuery;

            if (query == null)
            {
                throw new Exception("The Entity is Detached");
            }

            return(query.Context);
        }
コード例 #2
0
        public static object CreateOriginalValuesObjectWithReferences(this ObjectContext context, object source)
        {
            object    target = context.CreateOriginalValuesObject(source);
            EntityKey srcKey = ((IEntityWithKey)source).EntityKey;
            IEntityWithRelationships sourceWithRelationships = source as IEntityWithRelationships;

            if (sourceWithRelationships == null)
            {
                return(target);
            }

            foreach (var relationshipGroup in context.GetRelationshipsByRelatedEnd((IEntityWithKey)target,
                                                                                   EntityState.Unchanged | EntityState.Deleted))
            {
                IRelatedEnd tgtRelatedEnd = (IRelatedEnd)relationshipGroup.Key;
                foreach (ObjectStateEntry srcEntry in relationshipGroup)
                {
                    if (tgtRelatedEnd.IsEntityReference())
                    {
                        tgtRelatedEnd.SetEntityKey(srcEntry.OtherEndKey(srcKey));
                    }
                }
            }

            return(target);
        }
コード例 #3
0
 /// <summary>
 /// Atualiza os relacionamentos.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="newEntity"></param>
 /// <param name="oldEntity"></param>
 public static void ApplyReferencePropertyChanges(this ObjectContext context, IEntityWithRelationships newEntity, IEntityWithRelationships oldEntity)
 {
     foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
     {
         var oldRef = relatedEnd as EntityReference;
         if (oldRef == null) continue;
         var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;
         if (newRef != null) oldRef.EntityKey = newRef.EntityKey;
     }
 }
コード例 #4
0
        internal virtual IEntityWrapper WrapEntityUsingStateManagerGettingEntry(
            object entity,
            ObjectStateManager stateManager,
            out EntityEntry existingEntry)
        {
            IEntityWrapper wrapper = (IEntityWrapper)null;

            existingEntry = (EntityEntry)null;
            if (entity == null)
            {
                return(NullEntityWrapper.NullWrapper);
            }
            if (stateManager != null)
            {
                existingEntry = stateManager.FindEntityEntry(entity);
                if (existingEntry != null)
                {
                    return(existingEntry.WrappedEntity);
                }
                if (stateManager.TransactionManager.TrackProcessedEntities && stateManager.TransactionManager.WrappedEntities.TryGetValue(entity, out wrapper))
                {
                    return(wrapper);
                }
            }
            IEntityWithRelationships withRelationships = entity as IEntityWithRelationships;

            if (withRelationships != null)
            {
                RelationshipManager relationshipManager = withRelationships.RelationshipManager;
                if (relationshipManager == null)
                {
                    throw new InvalidOperationException(Strings.RelationshipManager_UnexpectedNull);
                }
                IEntityWrapper wrappedOwner = relationshipManager.WrappedOwner;
                if (!object.ReferenceEquals(wrappedOwner.Entity, entity))
                {
                    throw new InvalidOperationException(Strings.RelationshipManager_InvalidRelationshipManagerOwner);
                }
                return(wrappedOwner);
            }
            EntityProxyFactory.TryGetProxyWrapper(entity, out wrapper);
            if (wrapper == null)
            {
                IEntityWithKey entityWithKey = entity as IEntityWithKey;
                wrapper = EntityWrapperFactory.CreateNewWrapper(entity, entityWithKey == null ? (EntityKey)null : entityWithKey.EntityKey);
            }
            if (stateManager != null && stateManager.TransactionManager.TrackProcessedEntities)
            {
                stateManager.TransactionManager.WrappedEntities.Add(entity, wrapper);
            }
            return(wrapper);
        }
コード例 #5
0
        /// <summary>
        /// Gets the name of the entity container
        /// </summary>
        /// <param name="entity">entity</param>
        /// <returns>name of the entity container</returns>
        public static string GetEntityContainerName(this IEntityWithRelationships entity)
        {
            var result = string.Empty;

            if (entity != null)
            {
                var entitySet = GetEntitySet(entity.GetType());
                if (entitySet != null)
                {
                    result = entitySet.EntityContainer.Name;
                }
            }
            return(result);
        }
コード例 #6
0
 public void ApplyReferencePropertyChanges(
     IEntityWithRelationships newEntity,
     IEntityWithRelationships oldEntity)
 {
     foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
     {
         var oldRef = relatedEnd as EntityReference;
         if (oldRef != null)
         {
             // this related end is a reference not a collection
             var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;
             oldRef.EntityKey = newRef.EntityKey;
         }
     }
 }
コード例 #7
0
ファイル: EFHelper.cs プロジェクト: rajeshwarn/TestProject
        public static bool IsUnloadedEntityAssociation(object item, MemberInfo fieldOrProp)
        {
            Type propertyType = null;
            bool flag2;

            if (fieldOrProp is PropertyInfo)
            {
                propertyType = ((PropertyInfo)fieldOrProp).PropertyType;
            }
            else
            {
                if (!(fieldOrProp is FieldInfo))
                {
                    return(false);
                }
                propertyType = ((FieldInfo)fieldOrProp).FieldType;
            }
            EdmRelationshipNavigationPropertyAttribute attribute = (EdmRelationshipNavigationPropertyAttribute)fieldOrProp.GetCustomAttributes(typeof(EdmRelationshipNavigationPropertyAttribute), true).FirstOrDefault <object>();

            if (attribute == null)
            {
                return(false);
            }
            IEntityWithRelationships relationships = item as IEntityWithRelationships;

            if (relationships == null)
            {
                return(true);
            }
            Type       type2  = (flag2 = propertyType.IsGenericType && propertyType.Name.StartsWith("EntityCollection")) ? propertyType.GetGenericArguments()[0] : propertyType;
            string     str    = attribute.RelationshipNamespaceName + "." + attribute.RelationshipName;
            MethodInfo method = typeof(RelationshipManager).GetMethod(flag2 ? "GetRelatedCollection" : "GetRelatedReference", new Type[] { typeof(string), typeof(string) });

            if (method == null)
            {
                return(true);
            }
            object obj2 = method.MakeGenericMethod(new Type[] { type2 }).Invoke(relationships.RelationshipManager, new object[] { str, attribute.TargetRoleName });

            if (obj2 == null)
            {
                return(true);
            }
            Type         type3    = flag2 ? typeof(EntityCollection <>) : typeof(EntityReference <>);
            PropertyInfo property = type3.MakeGenericType(new Type[] { type2 }).GetProperty("IsLoaded");

            return((property == null) || !((bool)property.GetValue(obj2, null)));
        }
コード例 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="newEntity"></param>
        /// <param name="oldEntity"></param>
        public static void ApplyReferencePropertyChanges(this ObjectContext context, IEntityWithRelationships newEntity, IEntityWithRelationships oldEntity)
        {
            foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
            {
                var oldRef = relatedEnd as EntityReference;
                if (oldRef != null)
                {
                    // this related end is a reference not a collection
                    var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;

                    EntityObject refEntity = newRef.GetType().GetProperty("Value").GetValue(newRef, null) as EntityObject;
                    if (newRef.EntityKey == null && refEntity != null)
                    {
                        newRef.EntityKey = context.GetEntityKey(refEntity);
                    }

                    oldRef.EntityKey = newRef.EntityKey;
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// Wraps an entity and returns a new wrapper, or returns an existing wrapper if one
        /// already exists in the ObjectStateManager or in a RelationshipManager associated with
        /// the entity.
        /// </summary>
        /// <param name="entity">The entity to wrap</param>
        /// <param name="context">The state manager  in which the entity may exist, or null</param>
        /// <param name="existingEntry">The existing state entry for the given entity if one exists, otherwise null</param>
        /// <returns>A new or existing wrapper</returns>
        internal static IEntityWrapper WrapEntityUsingStateManagerGettingEntry(object entity, ObjectStateManager stateManager, out EntityEntry existingEntry)
        {
            Debug.Assert(!(entity is IEntityWrapper), "Object is an IEntityWrapper instance instead of the raw entity.");
            IEntityWrapper wrapper = null;

            existingEntry = null;

            if (entity == null)
            {
                return(NullEntityWrapper.NullWrapper);
            }
            // First attempt to find an existing wrapper in the ObjectStateMager.
            if (stateManager != null)
            {
                existingEntry = stateManager.FindEntityEntry(entity);
                if (existingEntry != null)
                {
                    return(existingEntry.WrappedEntity);
                }
                if (stateManager.TransactionManager.TrackProcessedEntities)
                {
                    if (stateManager.TransactionManager.WrappedEntities.TryGetValue(entity, out wrapper))
                    {
                        return(wrapper);
                    }
                }
            }
            // If no entity was found in the OSM, then check if one exists on an associated
            // RelationshipManager.  This only works where the entity implements IEntityWithRelationshops.
            IEntityWithRelationships entityWithRelationships = entity as IEntityWithRelationships;

            if (entityWithRelationships != null)
            {
                RelationshipManager relManager = entityWithRelationships.RelationshipManager;
                if (relManager == null)
                {
                    throw EntityUtil.UnexpectedNullRelationshipManager();
                }
                IEntityWrapper wrappedEntity = relManager.WrappedOwner;
                if (!Object.ReferenceEquals(wrappedEntity.Entity, entity))
                {
                    // This means that the owner of the RelationshipManager must have been set
                    // incorrectly in the call to RelationshipManager.Create().
                    throw EntityUtil.InvalidRelationshipManagerOwner();
                }
                return(wrappedEntity);
            }
            else
            {
                // Finally look to see if the instance is a proxy and get the wrapper from the proxy
                EntityProxyFactory.TryGetProxyWrapper(entity, out wrapper);
            }

            // If we could not find an existing wrapper, then go create a new one
            if (wrapper == null)
            {
                IEntityWithKey withKey = entity as IEntityWithKey;
                wrapper = CreateNewWrapper(entity, withKey == null ? null : withKey.EntityKey);
            }
            if (stateManager != null && stateManager.TransactionManager.TrackProcessedEntities)
            {
                stateManager.TransactionManager.WrappedEntities.Add(entity, wrapper);
            }
            return(wrapper);
        }
コード例 #10
0
        private static void ApplyReferencePropertyChanges(this ObjectContext context, IEntityWithRelationships newEntity, IEntityWithRelationships oldEntity)
        {
            foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
            {
                var oldReference = relatedEnd as EntityReference;

                if (oldReference != null)
                {
                    var newReference = newEntity.RelationshipManager.GetRelatedEnd(oldReference.RelationshipName,
                                                                                   oldReference.TargetRoleName) as EntityReference;

                    if (newReference != null)
                    {
                        oldReference.EntityKey = newReference.EntityKey;
                    }
                }
            }
        }
コード例 #11
0
        public void Attach(IEntityWithRelationships entity)
        {
            Contract.Requires(entity != null);

            throw new NotImplementedException();
        }
コード例 #12
0
        public bool Remove(IEntityWithRelationships entity)
        {
            Contract.Requires(entity != null);

            throw new NotImplementedException();
        }
コード例 #13
0
ファイル: EFExtension.cs プロジェクト: trburger/MyExtensions
 public static TContext GetEntities <TContext>(this IEntityWithRelationships entity) where TContext : class
 {
     return(GetContext(entity) as TContext);
 }