http://msdn.microsoft.com/en-us/library/dd756368.aspx
With the WCF Data Services client libraries, you can create, update, and delete entity data in a data service by performing equivalent actions on objects in the DataServiceContext. For more information, see Updating the Data Service (WCF Data Services).
The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart.
Example
// Create the DataServiceContext using the service URI. NorthwindEntities context = new NorthwindEntities(svcUri); // Create the new product. Product newProduct = Product.CreateProduct(0, "White Tea - loose", false); // Set property values. newProduct.QuantityPerUnit = "120gm bags"; newProduct.ReorderLevel = 5; newProduct.UnitPrice = 5.2M; try { // Add the new product to the Products entity set. context.AddToProducts(newProduct); // Send the insert to the data service. context.SaveChanges(); Console.WriteLine("New product added with ID {0}.", newProduct.ProductID); } catch (DataServiceRequestException ex) { throw new ApplicationException( "An error occurred when saving changes.", ex); }
The following example retrieves and modifies an existing object and then calls the UpdateObject method on the DataServiceContext to mark the item in the context as updated. An HTTP MERGE message is sent to the data service when the SaveChanges method is called.
string customerId = "ALFKI"; // Create the DataServiceContext using the service URI. NorthwindEntities context = new NorthwindEntities(svcUri); // Get a customer to modify using the supplied ID. var customerToChange = (from customer in context.Customers where customer.CustomerID == customerId select customer).Single(); // Change some property values. customerToChange.CompanyName = "Alfreds Futterkiste"; customerToChange.ContactName = "Maria Anders"; customerToChange.ContactTitle = "Sales Representative"; try { // Mark the customer as updated. context.UpdateObject(customerToChange); // Send the update to the data service. context.SaveChanges(); } catch (DataServiceRequestException ex) { throw new ApplicationException( "An error occurred when saving changes.", ex); }
The following example calls the DeleteObject method on the DataServiceContext to mark the item in the context as deleted. An HTTP DELETE message is sent to the data service when the SaveChanges method is called.
// Create the DataServiceContext using the service URI. NorthwindEntities context = new NorthwindEntities(svcUri); try { // Get the product to delete, by product ID. var deletedProduct = (from product in context.Products where product.ProductID == productID select product).Single(); // Mark the product for deletion. context.DeleteObject(deletedProduct); // Send the delete to the data service. context.SaveChanges(); } // Handle the error that occurs when the delete operation fails, // which can happen when there are entities with existing // relationships to the product being deleted. catch (DataServiceRequestException ex) { throw new ApplicationException( "An error occurred when saving changes.", ex); }
The following example creates a new object instance and then calls the AddRelatedObject method on the DataServiceContext to create the item in the context along with the link to the related order. An HTTP POST message is sent to the data service when the SaveChanges method is called.
int productId = 25; string customerId = "ALFKI"; Order_Detail newItem = null; // Create the DataServiceContext using the service URI. NorthwindEntities context = new NorthwindEntities(svcUri); try { // Get the specific product. var selectedProduct = (from product in context.Products where product.ProductID == productId select product).Single(); // Get the specific customer. var cust = (from customer in context.Customers.Expand("Orders") where customer.CustomerID == customerId select customer).Single(); // Get the first order. Order order = cust.Orders.FirstOrDefault(); // Create a new order detail for the specific product. newItem = Order_Detail.CreateOrder_Detail( order.OrderID, selectedProduct.ProductID, 10, 5, 0); // Add the new item with a link to the related order. context.AddRelatedObject(order, "Order_Details", newItem); context.SetLink(newItem, "Order", order); // Since the item is now tracked by the context, // set just the link to the related product. context.AddLink(selectedProduct, "Order_Details", newItem); context.SetLink(newItem, "Product", selectedProduct); // Add the new order detail to the collection, and // set the reference to the product. order.Order_Details.Add(newItem); newItem.Order = order; newItem.Product = selectedProduct; // Send the inserts to the data service. context.SaveChanges(); } catch (DataServiceQueryException ex) { throw new ApplicationException( "An error occurred when saving changes.", ex); } // Handle any errors that may occur during insert, such as // a constraint violation. catch (DataServiceRequestException ex) { throw new ApplicationException( "An error occurred when saving changes.", ex); }
'C#.NET 웹서비스' 카테고리의 다른 글
[SetEntitySetPageSize] WCF DataService 페이징 (0) | 2011.03.05 |
---|---|
[ AspNetCompatibilityRequirements ] 서비스가 ASP.NET 호환성을 지원하지 않으므로 서비스를 활성화할 수 없습니다. (0) | 2011.02.23 |
[DataService UploadFileCompleted Result ] 비동기 업로드 xml 결과값 받기 (0) | 2011.01.26 |
WCF DataService IIS 실행 (세팅) 안될때 확인 해볼 사항 (0) | 2011.01.25 |
WCF Data Services 퀵 스타트 // IIS Data Services 세팅 (0) | 2011.01.25 |
MSDN [WCF Data Services/DataSvcUtil.exe ]수동으로 클라이언트 데이터 서비스 클래스 생성 (0) | 2010.10.20 |
메시지를 역직렬화하는 동안 포맷터에서 예외가 발생했습니다. (0) | 2010.07.22 |
[ServiceThrottlingBehavior] 서비스 성능을 조정할 수 있는 런타임 처리량 설정을 구성 (0) | 2010.05.20 |
Duplex Services 참고 사이트 (0) | 2010.03.15 |
[MSDN] WCF단방향 호출, 콜백 및 이벤트에 대해 알아야 할 점 (0) | 2010.03.15 |