public void EFFK_Update_NarrowingUpdate1() { var cust = ctx.CreateQuery <EFFKClient.Customer>("CustomObjectContext.Customers").Select(c => new EFFKClient.NarrowCustomerWithFKOrder() { ID = c.ID, Orders = c.Orders.Select(o => new EFFKClient.NarrowOrderFKOnly() { ID = o.ID, CustomerId = o.CustomerId }).ToList() }).FirstOrDefault(); var order = cust.Orders.FirstOrDefault(); // set FK order.CustomerId = 1; ctx.UpdateObject(order); ctx.SaveChanges(); VerifyServerOrderId(order.ID, 1); // Add Link ctx.DetachLink(cust, "Orders", order); ctx.AddLink(cust, "Orders", order); ctx.SaveChanges(); VerifyServerOrderId(order.ID, cust.ID); // Add Link + Set FK order.CustomerId = 2; ctx.DetachLink(cust, "Orders", order); ctx.AddLink(cust, "Orders", order); ctx.SaveChanges(); VerifyServerOrderId(order.ID, cust.ID); }
/// <summary> /// Detach the tracked entity and related links from the <see cref="DataServiceContext"/>. /// </summary> /// <param name="context">The <see cref="DataServiceContext"/> containing the entity.</param> /// <param name="entity">The entity to detach.</param> public static void ClearTrackedEntity(this DataServiceContext context, object entity) { if (context == null) { throw new ArgumentNullException("context"); } if (entity == null) { throw new ArgumentNullException("entity"); } foreach (LinkDescriptor linkDescriptor in context.Links) { if (linkDescriptor.Source == entity || linkDescriptor.Target == entity) { context.DetachLink(linkDescriptor.Source, linkDescriptor.SourceProperty, linkDescriptor.Target); } } foreach (EntityDescriptor entityDescriptor in context.Entities) { if (entityDescriptor.Entity == entity) { context.Detach(entity); break; } } }
public static void DetachAll(this DataServiceContext context) { foreach (var descriptor in context.EntityTracker.Entities) { context.Detach(descriptor.Entity); } foreach (var link in context.EntityTracker.Links) { context.DetachLink(link.Source, link.SourceProperty, link.Target); } }
/// <summary> /// Resets the specified DataServiceContext object to remove all the entities. /// </summary> /// <param name="ctx">The DataServiceContext object.</param> public static void Reset(this DataServiceContext ctx) { foreach (LinkDescriptor link in ctx.Links) { ctx.DetachLink(link.Source, link.SourceProperty, link.Target); } foreach (EntityDescriptor entity in ctx.Entities) { ctx.Detach(entity.Entity); } }
public static void ClearContext(DataServiceContext context) { Debug.Assert(context != null, "context != null"); foreach (var link in context.Links) { context.DetachLink(link.Source, link.SourceProperty, link.Target); } foreach (var entity in context.Entities) { context.Detach(entity.Entity); } }
public static void AddOrAttachLink(this DataServiceContext context, object source, string propertyName, object target) { var descriptor = context.GetLinkDescriptor(source, propertyName, target); if (descriptor == null) { context.AddLink(source, propertyName, target); } else if (descriptor.State == EntityStates.Deleted) { context.DetachLink(source, propertyName, target); context.AttachLink(source, propertyName, target); } }