OData和插入(AddLink)多对多关系

时间:2022-10-05 13:28:40

Ok guys, this is driving me nuts. I got the following (odata) entity which represents a page structure in which every page can have any children and any parents. (basically a graph in which all nodes could be connected). In the database it's represented by a many-to-many relationship with two tables Page <-> PagePage <-> Page.

好了,伙计们,这真让我抓狂。我得到了下面的(odata)实体,它表示一个页面结构,在这个结构中,每个页面都可以有任何孩子和父母。(基本上是所有节点都可以连接的图)。在数据库中,它由两个表页<-> PagePage <->页的多对多关系表示。

OData和插入(AddLink)多对多关系

The problem is, I'm just not able to insert a new entity including a relation. The last thing I tried was:

问题是,我不能插入一个包含关系的新实体。我做的最后一件事是:

    Page page = new Page()
    {
        Id = Guid.NewGuid(),
        Title = "New Page",
        Created = DateTime.Now,
        LastChanged = DateTime.Now,
        IsRedirected = false,
        Position = 0,
        Html = "Add your HTML here.",
        Parent = { parent }
    };

    this.Context.AddToPages(page);
    this.Context.AddLink(parent, "Children", page);
    parent.Children.Add(page);
    this.Context.SaveChanges();

I can't think of anything else to try. Has anyone cracked this one?

我想不出别的办法来试试。有人破解过这个吗?

Edit 1: Here's a picture from the database diagram: OData和插入(AddLink)多对多关系

编辑1:这是来自数据库图的图片:

Edit 2: Exception Details:

编辑2:异常细节:

System.Data.Services.Client.DataServiceRequestException was unhandled
  Message=An error occurred while processing this request.
  Source=System.Data.Services.Client
  StackTrace:
       at System.Data.Services.Client.DataServiceContext.SaveResult.HandleBatchResponse()
       at System.Data.Services.Client.DataServiceContext.SaveResult.EndRequest()
       at System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
       at System.Data.Services.Client.DataServiceContext.SaveChanges()
       at PortfolioManagementConsole.Models.PageViewModel.SaveChanges(Object parameter) in C:\Users\Daniel\documents\visual studio 2010\Projects\PortfolioManagementService\PortfolioManagementConsole\Models\PageViewModel.cs:line 59
       at PortfolioManagementConsole.Common.RelayCommand.Execute(Object parameter) in C:\Users\Daniel\documents\visual studio 2010\Projects\PortfolioManagementService\PortfolioManagementConsole\Common\RelayCommand.cs:line 52
       at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at Telerik.Windows.Controls.RadButton.OnClick() in c:\Builds\WPF_Scrum\Release_WPF\Sources\Development\Core\Controls\Buttons\RadButton.cs:line 348
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at PortfolioManagementConsole.App.Main() in C:\Users\Daniel\documents\visual studio 2010\Projects\PortfolioManagementService\PortfolioManagementConsole\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Data.Services.Client.DataServiceClientException
       Message=<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code></code>
  <message xml:lang="de-CH">An error occurred while processing this request.</message>
</error>
       Source=System.Data.Services.Client
       StatusCode=500
       StackTrace:
            at System.Data.Services.Client.DataServiceContext.SaveResult.<HandleBatchResponse>d__1e.MoveNext()
       InnerException: 

1 个解决方案

#1


0  

Your WCF Data Services server is not returning detailed error messages and that's not helping you nail down the exact error message returned from the server. Follow these instructions to enable verbose error messages from the server : http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx . After you've enabled this , you should be able to see the error message being thrown by the server.

WCF数据服务服务器没有返回详细的错误消息,这也不能帮助您确定从服务器返回的确切错误消息。按照以下说明启用服务器的详细错误消息:http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net data .aspx。在启用此功能之后,您应该能够看到服务器抛出的错误消息。

From looking at your model, you need another link from the child back to the parent. Add this line to the code right before SaveChanges :

通过查看模型,您需要从子节点返回到父节点的另一个链接。在SaveChanges之前将这一行添加到代码中:

this.Context.SetLink(page, "Parent", parent);

this.Context。SetLink(页面,“父”,父母);

To help understand this better, here are some links on my blog where I detail how one works with associations using the WCF Data Services client :

为了更好地理解这一点,以下是我博客上的一些链接,我在这里详细介绍了如何使用WCF数据服务客户端与关联进行协作:

1 to many associations

1许多协会

http://blogs.msdn.com/b/phaniraj/archive/2008/10/23/working-with-associations-in-ado-net-data-services-part-2.aspx

http://blogs.msdn.com/b/phaniraj/archive/2008/10/23/working - -协会-在ado -网-数据-服务- - 2. aspx一部分

1 to 1 associations

1比1关联

http://blogs.msdn.com/b/phaniraj/archive/2008/07/02/working-with-relations-in-ado-net-data-services-beta-1.aspx

http://blogs.msdn.com/b/phaniraj/archive/2008/07/02/working - -关系-在ado -网-数据-服务-β- 1. aspx

#1


0  

Your WCF Data Services server is not returning detailed error messages and that's not helping you nail down the exact error message returned from the server. Follow these instructions to enable verbose error messages from the server : http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx . After you've enabled this , you should be able to see the error message being thrown by the server.

WCF数据服务服务器没有返回详细的错误消息,这也不能帮助您确定从服务器返回的确切错误消息。按照以下说明启用服务器的详细错误消息:http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net data .aspx。在启用此功能之后,您应该能够看到服务器抛出的错误消息。

From looking at your model, you need another link from the child back to the parent. Add this line to the code right before SaveChanges :

通过查看模型,您需要从子节点返回到父节点的另一个链接。在SaveChanges之前将这一行添加到代码中:

this.Context.SetLink(page, "Parent", parent);

this.Context。SetLink(页面,“父”,父母);

To help understand this better, here are some links on my blog where I detail how one works with associations using the WCF Data Services client :

为了更好地理解这一点,以下是我博客上的一些链接,我在这里详细介绍了如何使用WCF数据服务客户端与关联进行协作:

1 to many associations

1许多协会

http://blogs.msdn.com/b/phaniraj/archive/2008/10/23/working-with-associations-in-ado-net-data-services-part-2.aspx

http://blogs.msdn.com/b/phaniraj/archive/2008/10/23/working - -协会-在ado -网-数据-服务- - 2. aspx一部分

1 to 1 associations

1比1关联

http://blogs.msdn.com/b/phaniraj/archive/2008/07/02/working-with-relations-in-ado-net-data-services-beta-1.aspx

http://blogs.msdn.com/b/phaniraj/archive/2008/07/02/working - -关系-在ado -网-数据-服务-β- 1. aspx