情况就是这样。我正在使用ADP.NET Data Services 1.5 CTP2和Silverlight 3.我的EF数据模型(简而言之)是这样的:
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
设置为当前事件,为{{获取用户输入1}}和blammo - 新的链接记录。
在一个完美的世界中,我可以设置Profile
,整个过程将按预期工作。然而,由于扩张没有那么深,我不能。
作为一种解决方法,我将所有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
不会显示最近添加的记录。如果刷新应用程序,强制它重新读取数据库中的链接?它捡起来了。
所以我需要以下任何一种......
进行2级扩展的方法
最初的EventsGrid
记录
加载所以我可以简单地传递它的链接
属性到第二个网格
(保留上下文)
获得过滤的更好方法 不产生的“链接”的视图 一个独立的背景,没有 更新
通知“链接”的方法 它应该刷新的对象, 最好不要强迫它去 一路回到服务器 异步调用 - 因为数据清楚 已在当地更新 上下文。
任何提示?
答案 0 :(得分: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");
}
}