Esempio n. 1
0
        public void EFFK_Update_NarrowingUpdate1()
        {
            var cust = ctx.CreateQuery <EFFKClient.Customer>("CustomObjectContext.Customers").Select(c => new EFFKClient.NarrowCustomerWithFKOrder()
            {
                ID     = c.ID,
                Orders = c.Orders.Select(o => new EFFKClient.NarrowOrderFKOnly()
                {
                    ID         = o.ID,
                    CustomerId = o.CustomerId
                }).ToList()
            }).FirstOrDefault();

            var order = cust.Orders.FirstOrDefault();

            // set FK
            order.CustomerId = 1;
            ctx.UpdateObject(order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, 1);

            // Add Link
            ctx.DetachLink(cust, "Orders", order);
            ctx.AddLink(cust, "Orders", order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, cust.ID);

            // Add Link + Set FK
            order.CustomerId = 2;
            ctx.DetachLink(cust, "Orders", order);
            ctx.AddLink(cust, "Orders", order);
            ctx.SaveChanges();
            VerifyServerOrderId(order.ID, cust.ID);
        }
        public void SerializeEnity_TwoNavigationLinksInJsonFormat()
        {
            var person = new Person
            {
                ID   = 100,
                Name = "Bing",
            };

            var car1 = new Car {
                ID = 1001
            };
            var car2 = new Car {
                ID = 1002
            };

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri("http://www.odata.org/service.svc"));

            dataServiceContext.AttachTo("Persons", person);
            dataServiceContext.AttachTo("Cars", car1);
            dataServiceContext.AttachTo("Cars", car2);
            dataServiceContext.AddLink(person, "Cars", car1);
            dataServiceContext.AddLink(person, "Cars", car2);

            var requestInfo      = new RequestInfo(dataServiceContext);
            var serializer       = new Serializer(requestInfo);
            var headers          = new HeaderCollection();
            var clientModel      = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);

            entityDescriptor.State    = EntityStates.Added;
            entityDescriptor.Entity   = person;
            entityDescriptor.EditLink = new Uri("http://www.foo.com/custom");
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors            = new LinkDescriptor[] { new LinkDescriptor(person, "Cars", car1, clientModel), new LinkDescriptor(person, "Cars", car2, clientModel) };
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            // read result:
            MemoryStream stream = (MemoryStream)(odataRequestMessageWrapper.CachedRequestStream.Stream);

            stream.Position = 0;

            string payload = (new StreamReader(stream)).ReadToEnd();

            payload.Should().Be(
                "{\"ID\":100,\"Name\":\"Bing\",\"[email protected]\":[\"http://www.odata.org/service.svc/Cars(1001)\",\"http://www.odata.org/service.svc/Cars(1002)\"]}");
        }
Esempio n. 3
0
    public static void AddOrAttachLink(this DataServiceContext context, object source, string propertyName, object target)
    {
        var descriptor = context.GetLinkDescriptor(source, propertyName, target);

        if (descriptor == null)
        {
            context.AddLink(source, propertyName, target);
        }
        else if (descriptor.State == EntityStates.Deleted)
        {
            context.DetachLink(source, propertyName, target);
            context.AttachLink(source, propertyName, target);
        }
    }
        public void TestCollectionAdd()
        {
            var message = new Message
            {
                Id   = Guid.NewGuid(),
                Body = "Hi!",
                CC   = new List <Contact>()
            };

            var cc = new Contact
            {
                Email = "[email protected]",
                Name  = "def"
            };

            _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.First() == cc)))
            .AtMostOnce();

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

            var context = new DataServiceContext(ServiceUri);

            Playback(() =>
            {
                context.AttachTo("Contacts", cc);
                context.AttachTo("Messages", message);
                context.AddLink(message, "CC", cc);
                context.SaveChanges();
            });
        }
        private void InnerSubmit(DataServiceContext dataContext)
        {
            if (!string.IsNullOrWhiteSpace(this.TemplateId))
            {
                dataContext.AddObject(JobBaseCollection.JobSet, this);

                foreach (IAsset asset in this.InputMediaAssets)
                {
                    AssetData target = asset as AssetData;
                    if (target == null)
                    {
                        throw new ArgumentException(StringTable.ErrorInputTypeNotSupported);
                    }

                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, target);
                }
            }
            else
            {
                X509Certificate2 certToUse = null;
                Verify(this);

                dataContext.AddObject(JobBaseCollection.JobSet, this);

                List<AssetData> inputAssets = new List<AssetData>();
                AssetNamingSchemeResolver<AssetData, OutputAsset> assetNamingSchemeResolver = new AssetNamingSchemeResolver<AssetData, OutputAsset>(inputAssets);

                foreach (ITask task in ((IJob)this).Tasks)
                {
                    Verify(task);
                    TaskData taskData = (TaskData)task;

                    if (task.Options.HasFlag(TaskOptions.ProtectedConfiguration))
                    {
                        ProtectTaskConfiguration(taskData, ref certToUse, dataContext);
                    }

                    taskData.TaskBody = CreateTaskBody(assetNamingSchemeResolver, task.InputAssets.ToArray(), task.OutputAssets.ToArray());
                    taskData.InputMediaAssets.AddRange(task.InputAssets.OfType<AssetData>().ToArray());
                    taskData.OutputMediaAssets.AddRange(
                        task.OutputAssets
                            .OfType<OutputAsset>()
                            .Select(
                                c =>
                                {
                                    AssetData assetData = new AssetData { Name = c.Name, Options = (int)c.Options, AlternateId = c.AlternateId };
                                    assetData.InitCloudMediaContext(this._cloudMediaContext);

                                    return assetData;
                                })
                            .ToArray());
                    dataContext.AddRelatedObject(this, TasksPropertyName, taskData);
                }

                foreach (IAsset asset in inputAssets)
                {
                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, asset);
                }
            }
        }
 /// <summary>
 /// Adds the specified link to the set of objects the System.Data.Services.Client.DataServiceContext
 // is tracking.
 /// Remarks:
 ///     Notifies the context that a new link exists between the source and target
 ///     objects and that the link is represented via the source.sourceProperty which
 ///     is a collection.  The context adds this link to the set of newly created
 ///     links to be sent to the data service on the next call to SaveChanges().
 ///     Links are one way relationships. If a back pointer exists (ie. two way association),
 ///     this method should be called a second time to notify the context object of
 ///     the second link.
 /// </summary>
 /// <param name="source">The source object for the new link.</param>
 /// <param name="sourceProperty">The name of the navigation property on the source object that returns the related object.</param>
 /// <param name="target">The object related to the source object by the new link.</param>
 public void AddLink(object source, string sourceProperty, object target)
 {
     _dataContext.AddLink(source, sourceProperty, target);
 }
Esempio n. 7
0
        private void InnerSubmit(DataServiceContext dataContext)
        {
            if (!string.IsNullOrWhiteSpace(this.TemplateId))
            {
                dataContext.AddObject(JobBaseCollection.JobSet, this);

                foreach (IAsset asset in this.InputMediaAssets)
                {
                    AssetData target = asset as AssetData;
                    if (target == null)
                    {
                        throw new ArgumentException(StringTable.ErrorInputTypeNotSupported);
                    }

                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, target);
                }
            }
            else
            {
                X509Certificate2 certToUse = null;
                Verify(this);

                dataContext.AddObject(JobBaseCollection.JobSet, this);

                List <AssetData> inputAssets = new List <AssetData>();
                AssetNamingSchemeResolver <AssetData, OutputAsset> assetNamingSchemeResolver = new AssetNamingSchemeResolver <AssetData, OutputAsset>(inputAssets);

                foreach (ITask task in ((IJob)this).Tasks)
                {
                    Verify(task);
                    TaskData taskData = (TaskData)task;

                    if (task.Options.HasFlag(TaskOptions.ProtectedConfiguration))
                    {
                        ProtectTaskConfiguration(taskData, ref certToUse, dataContext);
                    }

                    taskData.TaskBody = CreateTaskBody(assetNamingSchemeResolver, task.InputAssets.ToArray(), task.OutputAssets.ToArray());
                    taskData.InputMediaAssets.AddRange(task.InputAssets.OfType <AssetData>().ToArray());
                    taskData.OutputMediaAssets.AddRange(
                        task.OutputAssets
                        .OfType <OutputAsset>()
                        .Select(
                            c =>
                    {
                        AssetData assetData = new AssetData {
                            Name = c.Name, Options = (int)c.Options, AlternateId = c.AlternateId
                        };
                        assetData.InitCloudMediaContext(this._cloudMediaContext);

                        return(assetData);
                    })
                        .ToArray());
                    dataContext.AddRelatedObject(this, TasksPropertyName, taskData);
                }

                foreach (IAsset asset in inputAssets)
                {
                    dataContext.AttachTo(AssetCollection.AssetSet, asset);
                    dataContext.AddLink(this, InputMediaAssetsPropertyName, asset);
                }
            }
        }
        public void SerializeEnity_TwoNavigationLinksInJsonFormat()
        {
            var person = new Person
            {
                ID = 100,
                Name = "Bing",
            };

            var car1 = new Car { ID = 1001 };
            var car2 = new Car { ID = 1002 };

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri("http://www.odata.org/service.svc"));
            dataServiceContext.AttachTo("Persons", person);
            dataServiceContext.AttachTo("Cars", car1);
            dataServiceContext.AttachTo("Cars", car2);
            dataServiceContext.AddLink(person, "Cars", car1);
            dataServiceContext.AddLink(person, "Cars", car2);

            var requestInfo = new RequestInfo(dataServiceContext);
            var serializer = new Serializer(requestInfo);
            var headers = new HeaderCollection();
            var clientModel = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);
            entityDescriptor.State = EntityStates.Added;
            entityDescriptor.Entity = person;
            entityDescriptor.EditLink = new Uri("http://www.foo.com/custom");
            var requestMessageArgs = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors = new LinkDescriptor[] { new LinkDescriptor(person, "Cars", car1, clientModel), new LinkDescriptor(person, "Cars", car2, clientModel)};
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            // read result:
            MemoryStream stream = (MemoryStream)(odataRequestMessageWrapper.CachedRequestStream.Stream);
            stream.Position = 0;

            string payload = (new StreamReader(stream)).ReadToEnd();
            payload.Should().Be(
                "{\"ID\":100,\"Name\":\"Bing\",\"[email protected]\":[\"http://www.odata.org/service.svc/Cars(1001)\",\"http://www.odata.org/service.svc/Cars(1002)\"]}");
        }