2010-01-21 38 views
1

所以情況就是這樣。我使用ADP.NET數據服務1.5 CTP2使用Silverlight 3。我的EF數據模型(簡稱)是這樣的:ADO.NET數據服務 - 在跟蹤列表中傳播更改?

CmsProfile 
     Id 
     Name 

CmsEvent 
     Id 
     Title 

CmsProfileEventLink 
     Id 
     ProfileId 
     EventId 

所以就有了很多< - 人與事件之間>一對多的關係。當我加載的Silverlight中的事件,我做這種方式:

private void AsyncLoadEventsKickoff() 
{ 
    DataServiceQuery<CmsEvent> theQuery = dashboardService 
     .CmsEvents 
     .Expand("CmsProfileEventLinks") 
     .AddQueryOption("$orderby", "Title"); 

    theQuery.BeginExecute(
     delegate(IAsyncResult asyncResult) 
     { 
      Dispatcher.BeginInvoke(
       () => 
       { 
        DataServiceQuery<CmsEvent> query = 
         asyncResult.AsyncState as DataServiceQuery<CmsEvent>; 
        if (query != null) 
        { 
         //create a tracked DataServiceCollection from the 
         //result of the asynchronous query. 
         events = DataServiceCollection 
          .CreateTracked<CmsEvent>(dashboardService, 
           query.EndExecute(asyncResult)); 

         AsyncLoadTracker(); 
        } 
       } 
      ); 
     }, 
     theQuery 
    ); 
} 

你會發現,我不能讓Expand()實際下降一個新的水平,讓我得到的事件鏈接的詳細信息。它只會真正告訴我是否有事件鏈接記錄。

我把所有的事件放入一個網格(SelectionGrid),當你點擊一個,我想加載另一個網格(EventsGrid)與這個事件相關的人。我通過加載CmsProfileEventLink對象加載網格,然後在DataMemberPath上深入查看配置文件名稱。理論上,這允許網格爲我添加新的鏈接 - 當添加一行時,我給它一個Id,並將CmsEvent設置爲當前事件,爲用戶輸入Profile和blammo - 新鏈接記錄。

在一個完美的世界中,我可以設置peopleGrid.ItemsSource = EventsGrid.Selecteditem.CmsPeopleEventLinks,整個事情將按預期工作。然而,由於擴張沒有那麼深,我不能。

作爲解決方法,我已經將所有的CmsProfileEventLinks都以同樣的方式加載到「鏈接」變量中。所以,當你選擇一個事件我這樣做(醜陋的,醜陋的,醜陋的),以顯示配置文件...

private void Sync_EventsGrid() 
{ 
    var item = SelectionGrid.SelectedItem as CmsEvent; 

    if (item.CmsEventProfileLinks != null) 
    { 
     DataServiceCollection<CmsEventProfileLink> x = 
      DataServiceCollection 
       .CreateTracked<CmsEventProfileLink>(
        dashboardService, 
        links.Where(p => p.CmsEvent == item)); 

     EventsGrid.ItemsSource = x; 
    } 
} 

問題是...如果一個變化中EventsGrid使其不會傳播回鏈接上下文,即使它們都共享DataService上下文。最終結果?如果您選擇其他事件並返回,EventsGrid不會顯示最近添加的記錄。如果刷新應用程序,迫使它重新讀取數據庫中的鏈接?它拿起它。

所以我需要以下任一...

  1. 辦法做到初始 負荷CmsEvent記錄的2級擴展 ,所以我可以簡單地通過它鏈接 屬性爲第二電網 (保留上下文)

  2. 一個更好的方式來獲得過濾 視圖的「鏈接」,不產卵 這並不一個獨立的上下文更新

  3. 通知「鏈接」 對象,它應該刷新方式, 最好不強迫它去 一路回服務器通過 異步調用 - 由於數據顯然 一直在當地 上下文中更新。

任何提示?

回答

1

你可以遍歷所有的擴展性能,並呼籲每一個LoadProperty擴大孩子,像這樣:

DataServiceQuery<CmsEvent> query 
         = asyncResult.AsyncState as DataServiceQuery<CmsEvent>; 
if (query != null) 
{ 
    //create a tracked DataServiceCollection from the 
    //result of the asynchronous query. 
    events = DataServiceCollection.CreateTracked<CmsEvent>(dashboardService, 
               query.EndExecute(asyncResult)); 
    AsyncLoadTracker(); 

    foreach(var link in events.SelectMany(e=> e.CmsProfileEventLink)) 
    { 
     dashboardService.LoadProperty(link, "CmsPeopleEventLinks"); 
    } 
}