The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Zero-To-Many Save Question

Last post 08-15-2008, 4:41 PM by David.Parsons. 1 replies.
Sort Posts: Previous Next
  •  08-15-2008, 8:42 AM 10761

    Zero-To-Many Save Question

    Maybe I am missing something here but some insight would be great.

    I am adding records to a parent, child, grandchild ES object. The first section of code works fine directly accessing the child but this is not what I need. I need to be able to work from the scope of the parent. If I try to run the second section of code I get a SourceId cannot be null in table TRNJ_OpsDocType error. When I debug the second section thisDocII gets its SourceId and ItemId automatically populated as soon as it is created, but testII does not, all fields are null.

    I have gone through the table structures to check everything and all looks well, maybe i am not understanding this hierarchial model correctly.

    Code:
                //This works
                TRNJOpsDoc thisDoc = new TRNJOpsDoc();
                thisDoc.LoadByPrimaryKey(1, 378291, 28);
                TRNJOpsDocType test = thisDoc.TRNJOpsDocTypeCollectionBySourceId.AddNew();
                test.DocTypeId = "BOL";
                thisDoc.Save();
    
                //This does not
                TRNJOpsItem opsItem = new TRNJOpsItem();
                opsItem.LoadByPrimaryKey(1, 378291);
                TRNJOpsDoc thisDocII = opsItem.TRNJOpsDocCollectionBySourceId.AddNew();
                TRNJOpsDocType testII = thisDocII.TRNJOpsDocTypeCollectionBySourceId.AddNew();
                testII.DocTypeId = "BOL";
                opsItem.Save();
    

    EntitySpaces Version # 2008.1.0623.0
    MyGeneration Version # 1.3.0.3

    Code:
    CREATE TABLE [dbo].[TRNJ_OpsItem](
    	[SourceId] [int] NOT NULL,
    	[ItemId] [int] NOT NULL,
     CONSTRAINT [PK_TRNJ_OpsItem] PRIMARY KEY CLUSTERED 
    (
    	[SourceId] ASC,
    	[ItemId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    ALTER TABLE [dbo].[TRNJ_OpsItem]  WITH CHECK ADD  CONSTRAINT [FK_TRNJ_OpsItem_TRNJ_DocSource] FOREIGN KEY([SourceId])
    REFERENCES [dbo].[TRNJ_DocSource] ([Id])
    ON UPDATE CASCADE
    GO
    ALTER TABLE [dbo].[TRNJ_OpsItem] CHECK CONSTRAINT [FK_TRNJ_OpsItem_TRNJ_DocSource]
    
    CREATE TABLE [dbo].[TRNJ_OpsDoc](
    	[SourceId] [int] NOT NULL,
    	[ItemId] [int] NOT NULL,
    	[Id] [int] IDENTITY(1,1) NOT NULL,
    	[Filename] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_TRNJ_OpsDocument_Filename]  DEFAULT (''),
     CONSTRAINT [PK_TRNJ_OpsDocument] PRIMARY KEY CLUSTERED 
    (
    	[SourceId] ASC,
    	[ItemId] ASC,
    	[Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDoc]  WITH CHECK ADD  CONSTRAINT [FK_TRNJ_OpsDoc_TRNJ_DocSource] FOREIGN KEY([SourceId])
    REFERENCES [dbo].[TRNJ_DocSource] ([Id])
    ON UPDATE CASCADE
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDoc] CHECK CONSTRAINT [FK_TRNJ_OpsDoc_TRNJ_DocSource]
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDoc]  WITH CHECK ADD  CONSTRAINT [FK_TRNJ_OpsDoc_TRNJ_OpsItem] FOREIGN KEY([SourceId], [ItemId])
    REFERENCES [dbo].[TRNJ_OpsItem] ([SourceId], [ItemId])
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDoc] CHECK CONSTRAINT [FK_TRNJ_OpsDoc_TRNJ_OpsItem]
    
    CREATE TABLE [dbo].[TRNJ_OpsDocType](
    	[SourceId] [int] NOT NULL,
    	[ItemId] [int] NOT NULL,
    	[Id] [int] NOT NULL,
    	[DocTypeId] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
     CONSTRAINT [PK_TRNJ_OpsDocTypes_1] PRIMARY KEY CLUSTERED 
    (
    	[SourceId] ASC,
    	[ItemId] ASC,
    	[Id] ASC,
    	[DocTypeId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    SET ANSI_PADDING OFF
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDocType]  WITH CHECK ADD  CONSTRAINT [FK_TRNJ_OpsDocType_TRNJ_DocType1] FOREIGN KEY([DocTypeId])
    REFERENCES [dbo].[TRNJ_DocType] ([Id])
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDocType] CHECK CONSTRAINT [FK_TRNJ_OpsDocType_TRNJ_DocType1]
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDocType]  WITH CHECK ADD  CONSTRAINT [FK_TRNJ_OpsDocTypes_TRNJ_OpsDoc] FOREIGN KEY([SourceId], [ItemId], [Id])
    REFERENCES [dbo].[TRNJ_OpsDoc] ([SourceId], [ItemId], [Id])
    GO
    ALTER TABLE [dbo].[TRNJ_OpsDocType] CHECK CONSTRAINT [FK_TRNJ_OpsDocTypes_TRNJ_OpsDoc]
    

    Stack Trace

    Code:
       at EntitySpaces.Core.esEntityCollection.SaveToProviderInsertsUpdates(esSqlAccessType sqlAccessType)
       at EntitySpaces.Core.esEntityCollection.SaveToProvider(esSqlAccessType sqlAccessType)
       at EntitySpaces.Core.esEntityCollection.Save(esSqlAccessType sqlAccessType)
       at EntitySpaces.Core.esEntityCollection.Save()
       at EntitySpaces.Core.esEntity.CommitPostSaves()
       at EntitySpaces.Core.esEntity.Save(esSqlAccessType sqlAccessType)
       at EntitySpaces.Core.esEntity.Save()
       at BusinessObjects.TRNJOpsDoc.Save() in C:\Documents and Settings\nstratton.trnj2\My Documents\Visual Studio 2008\Projects\EntitySpaces\EntitySpaces\EntitySpaces\Custom\TRNJOpsDoc.cs:line 120
       at DocApprover.ZoomImage.btnTest_Click(Object sender, RoutedEventArgs e) in C:\Documents and Settings\nstratton.trnj2\My Documents\Visual Studio 2008\Projects\DocApprover\DocApprover\ZoomImage.xaml.cs:line 918
       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(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.Controls.Button.OnClick()
       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(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(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, Int32 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, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       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.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at DocApprover.App.Main() in C:\Documents and Settings\nstratton.trnj2\My Documents\Visual Studio 2008\Projects\DocApprover\DocApprover\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly 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)
       at System.Threading.ThreadHelper.ThreadStart()

     

  •  08-15-2008, 4:41 PM 10766 in reply to 10761

    Re: Zero-To-Many Save Question

    It's difficult to look at a piece of the database, and understand the big picture. The most obvious point of confusion for me is that [TRNJ_OpsDoc] has an identity key, which is already unique, but it is just part of a composite PK. Fields do not have to be part of the primary key in order to participate as foreign keys.

    NOTE: The changes below are all just done on paper. They are not tested within SQL Server because of some missing table definitions required in the FK constraints, i.e., [TRNJ_DocSource] and [TRNJ_DocType].

    1) The first thing I would do is remove [SourceId] and [ItemId] from the PK definition of [TRNJ_OpsDoc].
    2) This changes the [TRNJ_OpsDocType] FK reference to [TRNJ_OpsDoc] to simply [Id].
    3) [TRNJ_OpsDocType] now seems de-normalized, with redundant [SourceId] and [ItemId] columns, so I would remove them entirely, from both the table and the PK. This essentially turns [TRNJ_OpsDocType] into a many-to-many associative table between [TRNJ_OpsDoc] and [TRNJ_DocType].


    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML