日期:2014-05-18 浏览次数:20944 次
// 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);
}