Ejemplo n.º 1
0
        public async Task SingletonUpdateInBatchTest()
        {
            var serviceRoot   = this.BaseAddress + "/clientTest/";
            var ClientContext = new Client.Container(new Uri(serviceRoot));

            // Reset data source
            await ClientContext.ExecuteAsync(new Uri(serviceRoot + "Umbrella/Microsoft.Test.E2E.AspNet.OData.Singleton.ResetDataSource"), "POST");

            await ClientContext.ExecuteAsync(new Uri(serviceRoot + "Partners/Microsoft.Test.E2E.AspNet.OData.Singleton.ResetDataSource"),
                                             "POST");

            var umbrella = await Task.Factory.FromAsync(ClientContext.Umbrella.BeginExecute(null, null), (asyncResult) =>
            {
                return(ClientContext.Umbrella.EndExecute(asyncResult).Single());
            });

            Client.Partner newPartner = new Client.Partner()
            {
                ID = 110, Name = "NewPartner"
            };

            umbrella.Name = "UpdatedCompanyName";
            ClientContext.UpdateObject(umbrella);
            ClientContext.AddRelatedObject(umbrella, "Partners", newPartner);

            await ClientContext.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset);
        }
Ejemplo n.º 2
0
        public async Task SingletonClientQueryTest()
        {
            var serviceRoot   = this.BaseAddress + "/clientTest/";
            var ClientContext = new Client.Container(new Uri(serviceRoot));

            ClientContext.MergeOption = MergeOption.OverwriteChanges;

            // Reset data source
            await ClientContext.ExecuteAsync(new Uri(serviceRoot + "Umbrella/Microsoft.Test.E2E.AspNet.OData.Singleton.ResetDataSource"), "POST");

            await ClientContext.ExecuteAsync(new Uri(serviceRoot + "Partners/Microsoft.Test.E2E.AspNet.OData.Singleton.ResetDataSource"),
                                             "POST");

            // Query
            var umbrella = await Task.Factory.FromAsync(ClientContext.Umbrella.BeginExecute(null, null), (asyncResult) =>
            {
                return(ClientContext.Umbrella.EndExecute(asyncResult).Single());
            });

            Assert.Equal("Umbrella", umbrella.Name);

            // Update and verify
            umbrella.Name = "UpdatedName";
            ClientContext.UpdateObject(umbrella);
            await ClientContext.SaveChangesAsync();

            var name = (await ClientContext.ExecuteAsync <string>(new Uri("Umbrella/Name", UriKind.Relative))).Single();

            Assert.Equal("UpdatedName", name);

            // $select
            var category = await Task.Factory.FromAsync(ClientContext.Umbrella.BeginExecute(null, null), (asyncResult) =>
            {
                return(ClientContext.Umbrella.EndExecute(asyncResult).Select(u => u.Category).Single());
            });

            Assert.Equal(Microsoft.Test.E2E.AspNet.OData.Singleton.Client.CompanyCategory.Communication, category);

            // Add navigation link
            var partner = new Client.Partner()
            {
                ID = 111, Name = "NewPartner1"
            };

            ClientContext.AddToPartners(partner);
            ClientContext.AddLink(umbrella, "Partners", partner);
            await ClientContext.SaveChangesAsync();

            var partner2 = new Client.Partner()
            {
                ID = 222, Name = "NewPartner2"
            };

            ClientContext.AddRelatedObject(umbrella, "Partners", partner2);
            await ClientContext.SaveChangesAsync();

            // Load navigation property
            await ClientContext.LoadPropertyAsync(umbrella, "Partners");

            Assert.NotNull(umbrella.Partners);

            // Add navigation target which is a singleton to entity
            partner = await Task.Factory.FromAsync(ClientContext.Partners.BeginExecute(null, null), (asyncResult) =>
            {
                return(ClientContext.Partners.EndExecute(asyncResult).Where(p => p.ID == partner2.ID).Single());
            });

            ClientContext.SetLink(partner, "Company", umbrella);
            await ClientContext.SaveChangesAsync();

            await ClientContext.LoadPropertyAsync(partner, "Company");

            Assert.NotNull(partner.Company);

            // Update singleton
            var navigatedCompany = partner.Company;

            navigatedCompany.Revenue = 100;
            ClientContext.UpdateRelatedObject(partner, "Company", navigatedCompany);
            await ClientContext.SaveChangesAsync();

            await ClientContext.LoadPropertyAsync(partner, "Company");

            navigatedCompany = partner.Company;
            Assert.Equal(100, navigatedCompany.Revenue);

            ClientContext.DeleteLink(umbrella, "Partners", partner);
            await ClientContext.SaveChangesAsync();

            var umbrellaQuery = ClientContext.Umbrella.Expand(u => u.Partners);

            umbrella = await Task.Factory.FromAsync(umbrellaQuery.BeginExecute(null, null), (asyncResult) =>
            {
                return(umbrellaQuery.EndExecute(asyncResult).Single());
            });

            Assert.Single(umbrella.Partners);
        }