Current filter:
                                You should refresh the page.

                                You are using an older version of the DevExpress Support Center.

                                  • Is there an example for XAF with an Entity Framework Self Referencing table with the child collection behaving in an aggregated manner
                                    i.e so the user does not have to re-pick the parent record when entering the child record.

                                    Although this is the third time I have asked about self referencing tables.this is not a duplicate question of .T278034 and T171380

                                    My problem can easily be repeated using the answer at T171380  and adding an Aggregated attribute to the children property..

                                    [C#]
                                    [Aggregated] public virtual IList<HCategory> Children { get; set; }

                                    The screencast shows what happens.

                                    https://www.screencast.com/t/wwTBAdzj

                                    I attempted a work around to initialize the parent using a NewObjectViewController

                                    [C#]
                                    public partial class H2CategoryController : ViewController { private NewObjectViewController controller; public H2CategoryController() { InitializeComponent(); TargetObjectType = typeof(H2Category); } protected override void OnActivated() { controller = Frame.GetController<NewObjectViewController>(); controller.ObjectCreated += controller_ObjectCreated; base.OnActivated(); } private void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) { var createdObject = e.CreatedObject as H2Category; var currentObject = View.CurrentObject as H2Category; // inspection here shows that currentObject and createdObject are different but they have the same proxy var s = $"created object Id is {createdObject.ID} current object id is {currentObject.ID}"; Console.WriteLine(s); } protected override void OnDeactivated() { controller.ObjectCreated += controller_ObjectCreated; base.OnDeactivated(); } }

                                    https://github.com/kgreed/SBD.GeneralLedger
                                    https://discourse.softwarebydesign.com.au/t/sbd-gl-general-ledger/1597

                                    Although the error is an entity framework multiplicity error, the screenshot at https://discourse.softwarebydesign.com.au/t/sbd-gl-general-ledger/1597/6   shows the issue is related to EFObjectSpace in the call stack.

                                    Maybe what I want is not possible?
                                    It seems annoying for the user to have to re-pick the parent record when ever they want to add a child.

                                2 Solutions

                                Creation Date Importance Sort by

                                Hello, Kirsten.

                                The relationship works incorrectly in your project because of a mistake in your H2Category class. The Parent property must be declared virtual. See Set a One-to-Many Relationship (EF). I have updated Dennis' answer in T171380 accordingly.
                                Also, your ICategoryController conflicts with the built-in TreeNodeController. This controller also subscribes to the NewObjectViewController.ObjectCreated event and links a newly created ITreeNode object with a parent node selected in the source list view. By default, objects are linked when changes are saved. To link the new object immediately upon creation, set the NewObjectViewController.LinkNewObjectToParentImmediately property to True. If you want to change how ITreeNode objects are linked, subscribe to the TreeNodeController.ProcessNewTreeNode event, link the objects as required, and set the e.Handled parameter to True.

                                • kirsten greed 01.14.2019

                                  Thanks Michael

                                Hello Kirsten,

                                Thank you for linking to your previous related tickets and other links as it helps us understand your requirements better.
                                We indeed do not have such an example, but your task looks feasible with the current framework capabilities.

                                >>i.e so the user does not have to re-pick the parent record when entering the child record.

                                Your approach with NewObjectViewController is a good start. I would just modify it to manipulate persistent objects within the same object space (for instance, using the IObjectSpace.GetObject method). I hope the following examples and explanations will help you move forward:
                                    EF - Agregated association reference property is null when creating new object
                                    EF - Parent object reference is not assigned immediately after a child node is created
                                    Visibility of linked property and the AggregateAttribute
                                    Entity Framework aggregated collection behavior and Link to parent in new object does not created with EF

                                With this Controller approach, you can also remove the Aggregated attribute completely so that it does not affect the layout. With that, the master property will be visible and initialized immediately. In addition, your end-users will have a chance to link an object to another parent at any moment later.

                                Show all comments
                                • kirsten greed 01.07.2019

                                  Thanks Dennis I am investigating further. I have also asked
                                  On Stack Overflow
                                  in case it is  an Entity Framework issue.

                                • kirsten greed 01.07.2019

                                  I think I found a work around, but it is very fragile.

                                  [C#]
                                  private void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) { var createdObject = e.CreatedObject as H2Category; // var numModifiedObjects =createdObject.ObjectSpace.ModifiedObjects.Count; var propertyCollectionSource = (View as ListView)?.CollectionSource as PropertyCollectionSource; if (!(propertyCollectionSource?.MasterObject is H2Category master)) return; var m = e.ObjectSpace.GetObject(master); createdObject.Parent = m; createdObject.Name = "t"; m.Children.Add(createdObject); // numModifiedObjects = createdObject.ObjectSpace.ModifiedObjects.Count; Console.WriteLine(master.ID); }

                                  If I uncomment the lines commented out here, then the error does not occur.

                                • kirsten greed 01.07.2019

                                  The presence of the line

                                  [C#]
                                  var numModifiedObjects =createdObject.ObjectSpace.ModifiedObjects.Count;

                                  prevents the error occurring.

                                  This seems too fragile to be called a solution.

                                • kirsten greed 01.07.2019

                                  I updated the source code at https://github.com/kgreed/SBD.GeneralLedger

                                • kirsten greed 01.09.2019

                                  Dennis, I believe my work around is needed because of a bug in Dev Express EF code.   

                                • Dennis (DevExpress) 01.09.2019

                                  I need additional time to research your project, Kirsten. I agree that this workaround is not good and there must be a reliable solution.

                                • kirsten greed 01.09.2019

                                  Thanks Dennis