//[TestMethod, Variation("One should not be able to get named streams via load property api")] public void NamedStreams_LoadPropertyTest() { // populate the context DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); EntityWithNamedStreams1 entity = context.CreateQuery<EntityWithNamedStreams1>("MySet1").Take(1).Single(); try { context.LoadProperty(entity, "Stream1"); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } try { context.BeginLoadProperty( entity, "Stream1", (result) => { context.EndLoadProperty(result); }, null); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } }
//[TestMethod, Variation("One should not be able to get named streams via load property api")] public void NamedStreams_LoadPropertyTest() { // populate the context DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); EntityWithNamedStreams1 entity = context.CreateQuery <EntityWithNamedStreams1>("MySet1").Take(1).Single(); try { context.LoadProperty(entity, "Stream1"); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } try { context.BeginLoadProperty( entity, "Stream1", (result) => { context.EndLoadProperty(result); }, null); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } }
/// <summary> /// Asynchronously saves this <see cref="IJobTemplate"/> when created from a copy of an existing <see cref="IJobTemplate"/>. /// </summary> /// <returns>A function delegate that returns the future result to be available through the Task.</returns> public Task SaveAsync() { if (!string.IsNullOrWhiteSpace(this.Id)) { // The job template was already saved, and there is no current support to update it. throw new InvalidOperationException(StringTable.InvalidOperationSaveForSavedJobTemplate); } DataServiceContext dataContext = this._cloudMediaContext.DataContextFactory.CreateDataServiceContext(); this.InnerSave(dataContext); return(dataContext .SaveChangesAsync(SaveChangesOptions.Batch, this) .ContinueWith( t => { t.ThrowIfFaulted(); JobTemplateData data = (JobTemplateData)t.AsyncState; dataContext.CreateQuery <JobTemplateData>(JobTemplateBaseCollection.JobTemplateSet).Where(jt => jt.Id == data.Id).First(); dataContext.LoadProperty(data, TaskTemplatesPropertyName); })); }
public virtual void CreateAndDeleteLinkToDerivedNavigationPropertyOnBaseEntitySet() { // clear respository this.ClearRepository("InheritanceTests_Vehicles"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var car = InstanceCreator.CreateInstanceOf <Car>(r); var vehicle = InstanceCreator.CreateInstanceOf <MiniSportBike>(r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject("InheritanceTests_Vehicles", car); ctx.AddObject("InheritanceTests_Vehicles", vehicle); ctx.SaveChanges(); ctx.SetLink(car, "SingleNavigationProperty", vehicle); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var cars = ctx.CreateQuery <Vehicle>("InheritanceTests_Vehicles").ToList().OfType <Car>(); var actual = cars.First(); ctx.LoadProperty(actual, "SingleNavigationProperty"); AssertExtension.PrimitiveEqual(vehicle, actual.SingleNavigationProperty); this.ClearRepository("InheritanceTests_Vehicles"); }
public void QueryExpandStronglyTypedProperties() { Uri baseUri = ctx.BaseUri; northwindClient.Products product = ctx.Execute <northwindClient.Products>(new Uri(baseUri.OriginalString + "/Products?$top=1")).Single <northwindClient.Products>(); Func <object, string, QueryOperationResponse> getResponse; for (int i = 0; i < 2; i++) { if (i == 0) { getResponse = (o, p) => { return(ctx.LoadProperty(o, p)); }; } else { // Async pattern getResponse = (o, p) => { IAsyncResult async = ctx.BeginLoadProperty(o, p, null, null); if (!async.CompletedSynchronously) { Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginLoadProperty timeout"); } Assert.IsTrue(async.IsCompleted); return(ctx.EndLoadProperty(async)); }; } int productID = ((QueryOperationResponse <int>)getResponse(product, "ProductID")).Single(); string productName = ((QueryOperationResponse <string>)getResponse(product, "ProductName")).Single(); short? reorderLevel = ((QueryOperationResponse <short?>)getResponse(product, "ReorderLevel")).Single(); northwindClient.Categories category = ((QueryOperationResponse <northwindClient.Categories>)getResponse(product, "Categories")).Single(); QueryOperationResponse <northwindClient.Order_Details> orderDetails = (QueryOperationResponse <northwindClient.Order_Details>)getResponse(product, "Order_Details"); Assert.IsTrue(orderDetails.Count() != 0, "There must be one or more order details"); } }
public virtual void AddAndRemoveDerivedNavigationPropertyInDerivedType() { // clear respository this.ClearRepository("InheritanceTests_Cars"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var car = InstanceCreator.CreateInstanceOf <Car>(r); var vehicle = InstanceCreator.CreateInstanceOf <MiniSportBike>(r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject("InheritanceTests_Cars", car); ctx.AddRelatedObject(car, "DerivedTypeNavigationProperty", vehicle); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); var actual = cars.ToList().First(); ctx.LoadProperty(actual, "DerivedTypeNavigationProperty"); AssertExtension.PrimitiveEqual(vehicle, actual.DerivedTypeNavigationProperty[0]); ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo("InheritanceTests_Cars", actual); ctx.AttachTo("InheritanceTests_Cars", actual.DerivedTypeNavigationProperty[0]); ctx.DeleteLink(actual, "DerivedTypeNavigationProperty", actual.DerivedTypeNavigationProperty[0]); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); actual = cars.ToList().First(); ctx.LoadProperty(actual, "DerivedTypeNavigationProperty"); Assert.Empty(actual.DerivedTypeNavigationProperty); this.ClearRepository("InheritanceTests_Cars"); }
/// <summary> /// Extension method to perform sync/async version of DataServiceContext.LoadProperty dynamically /// </summary> /// <param name="context">The context to call call load property on</param> /// <param name="continuation">The asynchronous continuation</param> /// <param name="async">A value indicating whether or not to use async API</param> /// <param name="entity">The entity to load a property on</param> /// <param name="propertyName">The name of the property to load</param> /// <param name="onCompletion">A callback for when the call completes</param> public static void LoadProperty(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string propertyName, Action <QueryOperationResponse> onCompletion) { ExceptionUtilities.CheckArgumentNotNull(context, "context"); AsyncHelpers.InvokeSyncOrAsyncMethodCall <QueryOperationResponse>( continuation, async, () => context.LoadProperty(entity, propertyName), c => context.BeginLoadProperty(entity, propertyName, c, null), r => context.EndLoadProperty(r), onCompletion); }
public void CRUDEntitySetShouldWork() { Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "UnicodeRouteTests_Todoü"; var uri = new Uri(this.BaseAddress + "/odataü"); // post new entity to repository var value = InstanceCreator.CreateInstanceOf <UnicodeRouteTests_Todoü>(r); var ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); IEnumerable <UnicodeRouteTests_Todoü> entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Nameü = InstanceCreator.CreateInstanceOf <string>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); var response = ctx.LoadProperty(afterUpdate, "Nameü"); Assert.Equal(200, response.StatusCode); // delete entity ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); Assert.Equal(0, entities.ToList().Count()); }
protected virtual void OnEditRowDialogClosed(object sender, EventArgs e) { ((DXWindow)sender).Closed -= OnEditRowDialogClosed; if ((bool)((DXWindow)sender).DialogResult) { DataServiceContext.UpdateObject(((Window)sender).Tag); DataServiceContext.SaveChanges(); UpdateDataSource(); } else { foreach (string propertyName in PropertiesList) { DataServiceContext.LoadProperty(((Window)sender).Tag, propertyName); } UpdateDataSource(); } }
//<snippetCustomersOrdersDeleteRelated> // Method that is called when the CollectionChanged event is handled. private bool OnCollectionChanged( EntityCollectionChangedParams entityCollectionChangedinfo) { if (entityCollectionChangedinfo.Action == NotifyCollectionChangedAction.Remove) { // Delete the related items when an order is deleted. if (entityCollectionChangedinfo.TargetEntity.GetType() == typeof(Order)) { // Get the context and object from the supplied parameter. DataServiceContext context = entityCollectionChangedinfo.Context; Order deletedOrder = entityCollectionChangedinfo.TargetEntity as Order; if (deletedOrder.Order_Details.Count == 0) { // Load the related OrderDetails. context.LoadProperty(deletedOrder, "Order_Details"); } // Delete the order and its related items; foreach (Order_Detail item in deletedOrder.Order_Details) { context.DeleteObject(item); } // Delete the order and then return true since the object is already deleted. context.DeleteObject(deletedOrder); return(true); } else { return(false); } } else { // Use the default behavior. return(false); } }
public void SDPC_QORFullLoad() { using (Utils.ConfigurationCacheCleaner()) using (Utils.RestoreStaticValueOnDispose(typeof(SimpleDataServiceHelper), "PageSizeCustomizer")) { SimpleDataServiceHelper.PageSizeCustomizer = PageSizeCustomizerFast; SimpleWorkspace workspace = this.NorthwindWorkspacePaged; Uri baseUri = new Uri(workspace.ServiceEndPoint + workspace.ServiceContainer.Name + ".svc", UriKind.Absolute); DataServiceContext ctx = new DataServiceContext(baseUri); ctx.EnableAtom = true; ctx.Format.UseAtom(); var q = ctx.CreateQuery <northwindBinding.Customers>("Customers").Expand("Orders"); int totalCustomerCount = q.Count(); int totalOrdersCount = ctx.CreateQuery <northwindBinding.Orders>("Orders").Count(); var qor = q.Execute() as QueryOperationResponse <northwindBinding.Customers>; DataServiceQueryContinuation <northwindBinding.Customers> nextCustLink = null; int custCount = 0; int orderCount = 0; do { ICollection previousOrderCollection = null; foreach (var c in qor) { try { if (previousOrderCollection != null) { qor.GetContinuation(previousOrderCollection); Assert.Fail("Out of scope collection did not throw"); } } catch (ArgumentException) { } var nextOrderLink = qor.GetContinuation(c.Orders); while (nextOrderLink != null) { if (custCount % 2 == 0) { var innerQOR = ctx.Execute <northwindBinding.Orders>(nextOrderLink) as QueryOperationResponse <northwindBinding.Orders>; foreach (var innerOrder in innerQOR) { ctx.AttachLink(c, "Orders", innerOrder); c.Orders.Add(innerOrder); } nextOrderLink = innerQOR.GetContinuation(); } else { nextOrderLink = ctx.LoadProperty(c, "Orders", nextOrderLink).GetContinuation(); } } previousOrderCollection = c.Orders; orderCount += c.Orders.Count; custCount++; } nextCustLink = qor.GetContinuation(); if (nextCustLink != null) { qor = ctx.Execute <northwindBinding.Customers>(nextCustLink) as QueryOperationResponse <northwindBinding.Customers>; } } while (nextCustLink != null); Assert.AreEqual(totalCustomerCount, custCount); Assert.AreEqual(totalOrdersCount, orderCount); Assert.AreEqual(totalOrdersCount, ctx.Links.Count); Assert.AreEqual(totalCustomerCount + totalOrdersCount, ctx.Entities.Count); } }
/// <summary> /// Loads deferred content for a specified property from the data service.Not /// supported by the WCF Data Services 5.0 client for Silverlight. /// Remarks: /// If entity is in in detached or added state, this method will throw an InvalidOperationException /// since there is nothing it can load from the server. If entity is in unchanged /// or modified state, this method will load its collection or reference elements /// as unchanged with unchanged bindings. If entity is in deleted state, this /// method will load the entities linked to by its collection or reference property /// in the unchanged state with bindings in the deleted state. /// </summary> /// <param name="entity">The entity that contains the property to load.</param> /// <param name="propertyName">The name of the property of the specified entity to load.</param> /// <returns>The response to the load operation.</returns> public QueryOperationResponse LoadProperty(object entity, string propertyName) { return(_dataContext.LoadProperty(entity, propertyName)); }
public void SDPC_QORFullLoad() { using (Utils.ConfigurationCacheCleaner()) using (Utils.RestoreStaticValueOnDispose(typeof(SimpleDataServiceHelper), "PageSizeCustomizer")) { SimpleDataServiceHelper.PageSizeCustomizer = PageSizeCustomizerFast; SimpleWorkspace workspace = this.NorthwindWorkspacePaged; Uri baseUri = new Uri(workspace.ServiceEndPoint + workspace.ServiceContainer.Name + ".svc", UriKind.Absolute); DataServiceContext ctx = new DataServiceContext(baseUri); ctx.EnableAtom = true; ctx.Format.UseAtom(); var q = ctx.CreateQuery<northwindBinding.Customers>("Customers").Expand("Orders"); int totalCustomerCount = q.Count(); int totalOrdersCount = ctx.CreateQuery<northwindBinding.Orders>("Orders").Count(); var qor = q.Execute() as QueryOperationResponse<northwindBinding.Customers>; DataServiceQueryContinuation<northwindBinding.Customers> nextCustLink = null; int custCount = 0; int orderCount = 0; do { ICollection previousOrderCollection = null; foreach (var c in qor) { try { if (previousOrderCollection != null) { qor.GetContinuation(previousOrderCollection); Assert.Fail("Out of scope collection did not throw"); } } catch (ArgumentException) { } var nextOrderLink = qor.GetContinuation(c.Orders); while (nextOrderLink != null) { if (custCount % 2 == 0) { var innerQOR = ctx.Execute<northwindBinding.Orders>(nextOrderLink) as QueryOperationResponse<northwindBinding.Orders>; foreach (var innerOrder in innerQOR) { ctx.AttachLink(c, "Orders", innerOrder); c.Orders.Add(innerOrder); } nextOrderLink = innerQOR.GetContinuation(); } else { nextOrderLink = ctx.LoadProperty(c, "Orders", nextOrderLink).GetContinuation(); } } previousOrderCollection = c.Orders; orderCount += c.Orders.Count; custCount++; } nextCustLink = qor.GetContinuation(); if (nextCustLink != null) { qor = ctx.Execute<northwindBinding.Customers>(nextCustLink) as QueryOperationResponse<northwindBinding.Customers>; } } while (nextCustLink != null); Assert.AreEqual(totalCustomerCount, custCount); Assert.AreEqual(totalOrdersCount, orderCount); Assert.AreEqual(totalOrdersCount, ctx.Links.Count); Assert.AreEqual(totalCustomerCount + totalOrdersCount, ctx.Entities.Count); } }