Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Professional ASP.NET Design Patterns
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2012, 10:04 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Delete Child From Aggregate Root

I am trying to delete a child object from an aggregate root but I keep receiving an error saying the foreign key is null.

I am using EF and the repository pattern and saving my aggregate root like below.

Code:
Dim product As New ProductInfo
product = _productRepository.FindBy(1).FirstOrDefault
If (product IsNot Nothing) Then
    stockTransfer.Remove(product.StockKeys.Where(Function(x) x.Id = 1).FirstOrDefault)
End If

_stockTransferRepository.Save(stockTransfer)
I have read the following two links.

http://blogs.msdn.com/b/dsimmons/arc...ps-in-ef4.aspx

http://mocella.blogspot.com/2010/01/...ect-graph.html

The links suggest to use DeleteObject to manually delete the child from the parent. Because when I remove the child from the parents collection it just removes the relationship but still leaves the record in the database.

My Save method in the repository is expecting an aggregate root. Will I need to create a new Save method inside my repository that passes in the aggregate root and the child object and then delete the child object manually while saving the aggregate root like below.

Code:
Public Sub SaveParentAndRemoveChild(entity As StockTransfer, item As StockTransferItem) Implements IStockTransferRepository.SaveParentAndRemoveChild
            GetObjectContext().DeleteObject(item)
            MyBase.Save(entity)
End Sub
Any suggestions would be great.

Mike
Reply With Quote
  #2 (permalink)  
Old February 25th, 2012, 06:29 AM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi mike,

Thanks for reading the book!

This is the issue you have http://stackoverflow.com/questions/2...-delete-it-why

Have a read of the answer in this post as I think it could solve your issue http://social.msdn.microsoft.com/For...3-bac661331788

I am not much of an EF expert I'm afraid. This is really easy to do in NHiberate though!

Cheers
Scott
Reply With Quote
  #3 (permalink)  
Old February 27th, 2012, 05:49 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response.

I am able to delete the child object using the DeleteObject() method but I was wondering which is the best way to follow DDD with deleting child objects with EF.

Should I create another method in my repository like DeleteChild() and call this before I use the save method?
Reply With Quote
  #4 (permalink)  
Old September 6th, 2012, 01:03 AM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where did you get to with this? I've also run into the same issue, and even adding a DeleteChild method on the repository is tricky because all the methods thereafter expect an AggregateRoot which its not.

I would be curious to know how this is done in NHibernate because it must be translatable.

I'm coming to the conclusion that a generic delete method needs to be added to the UOW which can delete any object. Practical, but defeats the architecture somewhat.

I had to do something similar for creating entities, since entities need to be created by the calling CreateObject for the data context to register them I added T CreateObject<t>() to my UOW to handle non-aggreate root entities.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find child node when there is mentioned (child::*) mlohokare XSLT 3 May 12th, 2009 12:40 PM
aggregate function polofson BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 0 August 14th, 2007 01:29 PM
How to use the SUM Aggregate gregalb SQL Server 2000 1 May 10th, 2007 01:19 AM
DataShaping with Aggregate Jon4Paz Classic ASP Databases 0 September 15th, 2006 01:29 PM
delete XML node from JavaScript - not child crmpicco Javascript How-To 1 April 27th, 2006 11:42 AM



All times are GMT -4. The time now is 12:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.