public void EFFK_DeleteLinks()
        {
            var cust  = ctx.CreateQuery <EFFKClient.Customer>("CustomObjectContext.Customers").Expand("Orders($expand=Customers)").FirstOrDefault();
            var order = cust.Orders.FirstOrDefault();

            // delete link via FK
            order.CustomerId = null;
            ctx.UpdateObject(order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, null);

            order.CustomerId = cust.ID;
            ctx.UpdateObject(order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, cust.ID);

            // delete link via Set Link
            ctx.SetLink(order, "Customers", null);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, null);

            order.CustomerId = cust.ID;
            ctx.UpdateObject(order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, cust.ID);

            // delete link via DeleteLink
            ctx.DeleteLink(cust, "Orders", order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, null);
        }
Exemple #2
0
    public static void DeleteOrDetachLink(this DataServiceContext context, object source, string propertyName, object target)
    {
        var descriptor = context.GetLinkDescriptor(source, propertyName, target);

        if (descriptor == null)
        {
            context.AttachLink(source, propertyName, target);
            context.DeleteLink(source, propertyName, target);
        }
        else if (descriptor.State == EntityStates.Added)
        {
            context.DetachLink(source, propertyName, target);
        }
        else
        {
            context.DeleteLink(source, propertyName, target);
        }
    }
        public void TestCollectionDelete()
        {
            var cc = new Contact
            {
                Email = "[email protected]",
                Name  = "def"
            };

            var message = new Message
            {
                Id = Guid.NewGuid(),
                CC = new List <Contact> {
                    cc
                }
            };

            _resourceFinderMock.Setup(
                resourceFinder => resourceFinder.GetResource(It.IsAny <IQueryable <Message> >(), null))
            .Returns(message)
            .AtMostOnce();

            _resourceFinderMock.Setup(
                resourceFinder => resourceFinder.GetResource(It.IsAny <IQueryable <Contact> >(), null))
            .Returns(cc)
            .AtMostOnce();

            Setup(session => session.Store(It.Is <Message>(actual => actual == message && actual.CC.Count == 0)))
            .AtMostOnce();

            Setup(session => session.Commit())
            .AtMostOnce();

            var context = new DataServiceContext(ServiceUri);

            Playback(() =>
            {
                context.AttachTo("Contacts", cc);
                context.AttachTo("Messages", message);

                context.DeleteLink(message, "CC", cc);

                context.SaveChanges();
            });
        }
Exemple #4
0
        public virtual void AddAndRemoveBaseNavigationPropertyInDerivedType()
        {
            // clear respository
            this.ClearRepository("InheritanceTests_Cars");

            Random r = new Random(RandomSeedGenerator.GetRandomSeed());

            // post new entity to repository
            CreatorSettings creatorSettings = new CreatorSettings()
            {
                NullValueProbability = 0,
            };
            var car                = InstanceCreator.CreateInstanceOf <Car>(r, creatorSettings);
            var vehicle            = InstanceCreator.CreateInstanceOf <Vehicle>(r, creatorSettings);
            DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);

            ctx.AddObject("InheritanceTests_Cars", car);
            ctx.AddRelatedObject(car, "BaseTypeNavigationProperty", vehicle);
            ctx.SaveChangesAsync().Wait();

            ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);
            var cars   = ctx.CreateQuery <Car>("InheritanceTests_Cars");
            var actual = cars.ExecuteAsync().Result.First();

            ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait();

            AssertExtension.PrimitiveEqual(vehicle, actual.BaseTypeNavigationProperty[0]);

            ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);
            ctx.AttachTo("InheritanceTests_Cars", actual);
            ctx.AttachTo("InheritanceTests_Vehicles", actual.BaseTypeNavigationProperty[0]);
            ctx.DeleteLink(actual, "BaseTypeNavigationProperty", actual.BaseTypeNavigationProperty[0]);
            ctx.SaveChangesAsync().Wait();

            ctx    = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4);
            cars   = ctx.CreateQuery <Car>("InheritanceTests_Cars");
            actual = cars.ExecuteAsync().Result.First();
            ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait();

            Assert.Empty(actual.BaseTypeNavigationProperty);

            this.ClearRepository("InheritanceTests_Cars");
        }
 /// <summary>
 /// Changes the state of the link to deleted in the list of links being tracked
 /// by the System.Data.Services.Client.DataServiceContext.
 /// Remarks:
 ///     Notifies the context that a link exists between the source and target object
 ///     and that the link is represented via the source.sourceProperty which is a
 ///     collection.  The context adds this link to the set of deleted links to be
 ///     sent to the data service on the next call to SaveChanges().  If the specified
 ///     link exists in the "Added" state, then the link is detached (see DetachLink
 ///     method) instead.
 /// </summary>
 /// <param name="source">The source object in the link to be marked for deletion.</param>
 /// <param name="sourceProperty">The name of the navigation property on the source object that is used to access the target object.</param>
 /// <param name="target">The target object involved in the link that is bound to the source object.
 /// The target object must be of the type identified by the source property or
 /// a subtype.</param>
 public void DeleteLink(object source, string sourceProperty, object target)
 {
     _dataContext.DeleteLink(source, sourceProperty, target);
 }