Ejemplo n.º 1
0
        /// <summary>
        /// Detach the tracked entity and related links from the <see cref="DataServiceContext"/>.
        /// </summary>
        /// <param name="context">The <see cref="DataServiceContext"/> containing the entity.</param>
        /// <param name="entity">The entity to detach.</param>
        public static void ClearTrackedEntity(this DataServiceContext context, object entity)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            foreach (LinkDescriptor linkDescriptor in context.Links)
            {
                if (linkDescriptor.Source == entity || linkDescriptor.Target == entity)
                {
                    context.DetachLink(linkDescriptor.Source, linkDescriptor.SourceProperty, linkDescriptor.Target);
                }
            }

            foreach (EntityDescriptor entityDescriptor in context.Entities)
            {
                if (entityDescriptor.Entity == entity)
                {
                    context.Detach(entity);
                    break;
                }
            }
        }
 static void ReadETagsAndDetach(DataServiceContext context, Action <object, string> write)
 {
     foreach (var entity in context.Entities)
     {
         write(entity.Entity, entity.ETag);
         context.Detach(entity.Entity);
     }
 }
Ejemplo n.º 3
0
 public static void DetachAll(this DataServiceContext context)
 {
     foreach (var descriptor in context.EntityTracker.Entities)
     {
         context.Detach(descriptor.Entity);
     }
     foreach (var link in context.EntityTracker.Links)
     {
         context.DetachLink(link.Source, link.SourceProperty, link.Target);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Resets the specified DataServiceContext object to remove all the entities.
 /// </summary>
 /// <param name="ctx">The DataServiceContext object.</param>
 public static void Reset(this DataServiceContext ctx)
 {
     foreach (LinkDescriptor link in ctx.Links)
     {
         ctx.DetachLink(link.Source, link.SourceProperty, link.Target);
     }
     foreach (EntityDescriptor entity in ctx.Entities)
     {
         ctx.Detach(entity.Entity);
     }
 }
Ejemplo n.º 5
0
        public static void ClearContext(DataServiceContext context)
        {
            Debug.Assert(context != null, "context != null");
            foreach (var link in context.Links)
            {
                context.DetachLink(link.Source, link.SourceProperty, link.Target);
            }

            foreach (var entity in context.Entities)
            {
                context.Detach(entity.Entity);
            }
        }
Ejemplo n.º 6
0
        public static T CreateEntity <T>(DataServiceContext ctx, string entitySetName, EntityStates state, DataServiceQuery <T> query) where T : class, new()
        {
            T entity = null;

            try
            {
                switch (state)
                {
                case EntityStates.Added:
                    entity = new T();
                    ctx.AddObject(entitySetName, entity);
                    break;

                case EntityStates.Deleted:
                    entity = CreateEntity(ctx, entitySetName, EntityStates.Unchanged, query);
                    ctx.DeleteObject(entity);
                    break;

                case EntityStates.Detached:
                    entity = query.Execute().Single();
                    Assert.AreEqual(MergeOption.NoTracking != ctx.MergeOption, ctx.Detach(entity));
                    break;

                case EntityStates.Unchanged:
                    entity = query.Execute().Single();
                    if (MergeOption.NoTracking == ctx.MergeOption)
                    {
                        ctx.AttachTo(entitySetName, entity);
                    }

                    break;

                case EntityStates.Modified:
                    entity = CreateEntity(ctx, entitySetName, EntityStates.Unchanged, query);
                    ctx.UpdateObject(entity);
                    break;

                default:
                    Assert.Fail(String.Format("unexpected state encountered: {0}", state));
                    break;
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("{0}", ex);
            }

            return(entity);
        }
        private static void AttachAndLog(StringBuilder output, DataServiceContext ctx, string entitySetName, object entity)
        {
            try
            {
                ctx.AttachTo(entitySetName, entity);

                EntityDescriptor entityDescriptor = ctx.Entities[0];
                entityDescriptor.Identity.Should().Be(entityDescriptor.EditLink.AbsoluteUri);
                output.AppendLine(entityDescriptor.EditLink.OriginalString);

                ctx.Detach(entity);
            }
            catch (Exception e)
            {
                var exception = e;
                while (exception != null)
                {
                    output.Append(exception.GetType().FullName);
                    output.Append(": ");
                    output.AppendLine(exception.Message);
                    exception = exception.InnerException;
                }
            }
        }
        private static void AttachAndLog(StringBuilder output, DataServiceContext ctx, string entitySetName, object entity)
        {
            try
            {
                ctx.AttachTo(entitySetName, entity);

                EntityDescriptor entityDescriptor = ctx.Entities[0];
                entityDescriptor.Identity.Should().Be(entityDescriptor.EditLink.AbsoluteUri);
                output.AppendLine(entityDescriptor.EditLink.OriginalString);

                ctx.Detach(entity);
            }
            catch (Exception e)
            {
                var exception = e;
                while (exception != null)
                {
                    output.Append(exception.GetType().FullName);
                    output.Append(": ");
                    output.AppendLine(exception.Message);
                    exception = exception.InnerException;
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Applies this state to the specfied <paramref name="target"/> such that after invocation, 
        /// the target in the given <paramref name="context"/> is in this state.
        /// </summary>
        /// <param name="context">Context to apply changes to.</param>
        /// <param name="target">Target to change state on.</param>
        /// <param name="entitySetName">Name of entity set (necessary for certain transitions).</param>
        public void ApplyToObject(DataServiceContext context, object target, string entitySetName)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            EntityStates current = GetStateForEntity(context, target);
            if (current == this.state)
            {
                return;
            }

            switch (this.state)
            {
                case EntityStates.Added:
                    if (current != EntityStates.Detached)
                    {
                        context.Detach(target);
                    }

                    context.AddObject(entitySetName, target);
                    break;
                case EntityStates.Detached:
                    context.Detach(target);
                    break;
                case EntityStates.Deleted:
                    if (current == EntityStates.Detached)
                    {
                        context.AttachTo(entitySetName, target);
                    }

                    context.DeleteObject(target);
                    break;
                case EntityStates.Modified:
                    if (current == EntityStates.Detached)
                    {
                        context.AttachTo(entitySetName, target);
                    }

                    context.UpdateObject(target);
                    break;
                case EntityStates.Unchanged:
                    if (current != EntityStates.Detached)
                    {
                        context.Detach(target);
                    }

                    context.AttachTo(entitySetName, target);
                    break;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Applies this state to the specfied <paramref name="target"/> such that after invocation,
        /// the target in the given <paramref name="context"/> is in this state.
        /// </summary>
        /// <param name="context">Context to apply changes to.</param>
        /// <param name="target">Target to change state on.</param>
        /// <param name="entitySetName">Name of entity set (necessary for certain transitions).</param>
        public void ApplyToObject(DataServiceContext context, object target, string entitySetName)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            EntityStates current = GetStateForEntity(context, target);

            if (current == this.state)
            {
                return;
            }

            switch (this.state)
            {
            case EntityStates.Added:
                if (current != EntityStates.Detached)
                {
                    context.Detach(target);
                }

                context.AddObject(entitySetName, target);
                break;

            case EntityStates.Detached:
                context.Detach(target);
                break;

            case EntityStates.Deleted:
                if (current == EntityStates.Detached)
                {
                    context.AttachTo(entitySetName, target);
                }

                context.DeleteObject(target);
                break;

            case EntityStates.Modified:
                if (current == EntityStates.Detached)
                {
                    context.AttachTo(entitySetName, target);
                }

                context.UpdateObject(target);
                break;

            case EntityStates.Unchanged:
                if (current != EntityStates.Detached)
                {
                    context.Detach(target);
                }

                context.AttachTo(entitySetName, target);
                break;
            }
        }
Ejemplo n.º 11
0
        public  static void ClearContext(DataServiceContext context)
        {
            Debug.Assert(context != null, "context != null");
            foreach (var link in context.Links)
            {
                context.DetachLink(link.Source, link.SourceProperty, link.Target);
            }

            foreach (var entity in context.Entities)
            {
                context.Detach(entity.Entity);
            }
        }