January '10
6. The GetOrder methods should use the Include operator to eager-load order details and products. 7. In the SaveOrder method call ApplyChanges on ctx.Orders, passing the incoming Order entity (inserts and updates). 8. In the DeleteOrder method mark order and order details as deleted, then call ApplyChanges. 9. Add a client application to the project. 10. In order to delete an order detail, it is not sufficient to simply remove it from the order. Rather, you will need to call MarkAsDeleted on the order detail. If you experimented with self-tracking entities in the first EF 4.0 CTP, based on Beta 1 of .NET 4.0 and Visual Studio 2010, you may have noticed a different method signaturefor ApplyChanges, in which you had to pass a delegate the returned an EntityChangeTrackerAdapter. While allowed you to supply your own adapter for converting change-state, the method signature for ApplyChanges is simpler and easier to use. CTP 2 also improved the story of self-tracking entities by taking code for relationship fix-up out of the collection and placing it in the generated entity classes, where it takes advantage of the foreign key associations. The T4 templates for STEs also provide better support for concurrency management by preserving original values for properties that specify a ConcurrencyMode of Fixed in the entity data model. And the AcceptChanges method makes it easier to reinitialize an entity to an unmodified state after it’s been updated. Hats off to the EF team for tackling n-tier scenarios head-on and coming up with an end-to-end solution for using the Entity Framework in service-oriented applications. Keep up the good work! Tony is Author of DevelopMentor's new course "LINQ with the Entity Framework 4.0"
[ServiceContract]
public interface INorthwindService
{
[OperationContract]
Product GetProduct(int productId);
[OperationContract]
Order GetOrder(int orderId);
[OperationContract]
Order SaveOrder(Order order);
[OperationContract]
void DeleteOrder(Order order);
}public Order GetOrder(int orderId)
{
using (NorthwindEntities ctx = new NorthwindEntities())
{
// Eager-load related entities
var query =
from o in ctx.Orders
.Include("OrderDetails.Product")
where o.OrderID == orderId
select o;
Order order = query.SingleOrDefault();
return order;
}
}
public Order SaveOrder(Order order)
{
using (NorthwindEntities ctx = new NorthwindEntities())
{
// Inform object state mgr of changes
ctx.Orders.ApplyChanges(order);
// Persist changes to database
ctx.SaveChanges();
// Accept changes for order and details
order.AcceptChanges();
foreach (OrderDetail od in order.OrderDetails)
od.AcceptChanges();
return order;
}
}
public static void DeleteOrder(Order order)
{
using (NorthwindEntities ctx = new NorthwindEntities
(Settings.Default.NorthwindConnection))
{
// First mark details as deleted (from a list)
foreach (OrderDetail od in order.OrderDetails.ToList())
{
od.MarkAsDeleted();
}
// Then mark order as deleted
order.MarkAsDeleted();
// Inform object state mgr of changes
ctx.Orders.ApplyChanges(order);
// Persist changes to database
ctx.SaveChanges();
}
}
Rod da Silva is the most knowledgable instructor I've ever had in my 10 years of IT experience. Frank J.