2012-06-25 68 views
0

由於包含子集,與歸屬父:檢測複合子項刪除

[Include, Composition] 
public virtual ICollection<FieldValue> FieldValues {get;set;} 

,做負載:

Context.Load(Context.GetQuery(),LoadBehavior.RefreshCurrent,true); 

我看到FieldValues其他用戶刪除或後臺進程不會從客戶端上下文中刪除。我可以看到這是如何設計的,但我該如何解決這個問題?有沒有辦法插入到加載過程來處理刪除的項目?

加載回調發生得太晚了,它的外觀 - 所有東西都已經合併了,沒有什麼可比較的。

回答

1

找出來了。給定一個父類的根,這是運行負載方式:

var originalValues = Context.EntityContainer 
    .GetEntitySet<FieldValue>() 
    .ToList(); // <-- make the pre-load copy of the child entities 

Context.Load(Context.GetQuery(), 
    LoadBehavior.RefreshCurrent, 
    (LoadOperation<Root> o) => 
    { 
     (from v in originalValues 
     join nv in o.AllEntities.OfType<FieldValue>() on v equals nv into g 
     from existing in g.DefaultIfEmpty() 
     where existing == null 
     select v) 
     .ToList() 
     .ForEach(Context.EntityContainer.GetEntitySet<FieldValue>().Detach); 
    }, 
    null); 

注:fieldValue方法實現IEquatable <>,使用PK,如果你的孩子沒有