我在Windows Phone 7中使用WCF数据服务,我想单击保存关系数据我该怎么做?
它认为2表:类别和产品
我想保存数据UI:
来自Cateogry表: -
CategoryId :(自动增量)
CategoryName:abc从产品表: -
ProductId :-(自动增量)
CategoryId: - ? (不知道我怎么能找回)
产品名称:xyz
按钮保存点击:
我想在适当的表格中插入上述数据,我该怎么做?
我使用以下代码添加一个表数据:
try
{
context = new NorthwindEntities(NorthwindUri);
context.AddToProducts(product);
context.BeginSaveChanges(new AsyncCallback((result) =>
{
bool errorOccured = false;
// Use the Dispatcher to ensure that the
// asynchronous call returns in the correct thread.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
context = result.AsyncState as NorthwindEntities;
try
{
// Complete the save changes operation and display the response.
DataServiceResponse response = context.EndSaveChanges(result);
foreach (ChangeOperationResponse changeResponse in response)
{
if (changeResponse.Error != null) errorOccured = true;
}
if (!errorOccured)
{
MessageBox.Show("The changes have been saved to the data service.");
}
else
{
MessageBox.Show("An error occured. One or more changes could not be saved.");
}
}
catch (Exception ex)
{
// Display the error from the response.
MessageBox.Show(string.Format("The following error occured: {0}", ex.Message));
}
});
}), context);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("The changes could not be saved to the data service.\n"
+ "The following error occurred: {0}", ex.Message));
}
答案 0 :(得分:1)
在OData中,关系不表示为外键,而是表示为导航属性。然后通过操纵客户端库中的链接来操纵它们。 看一下这篇文章:http://msdn.microsoft.com/en-us/library/dd756361(v=vs.103).aspx 您可以调用多个修改数据的方法,然后调用SaveChanges将它们全部发送到服务器。 但请注意,如果服务器需要参照完整性并且您例如同时添加两个相关实体,则可能需要使用SaveChanges(Batch)(这使得客户端在一个请求中发送所有内容,从而允许服务器将其作为单一交易处理。)