Knight Data Access Layer
Save with Children VB.NET
Basic Tasks VB.NET > Saving Data > Save with Children VB.NET

Normal save methods do not save parents and children.  Use the SaveWithChildren to save all parents and children.  It will recursively parse the object tree, properly saving parents before saving children.  It can handle many to many relationships.  The save is automatically wrapped in a transaction.

 

 

'Get the current instance of the helper

Dim db As IDataHelper = DataHelper.SessionFactory()

 

'Create an Order with a Parent and Children

Dim order As New Order()

order.Buyer = New Person()

order.Buyer.FirstName = "John"

order.Buyer.LastName = "Smith"

order.Buyer.DateCreated = Date.Now

order.OrderDetails = New List(Of OrderDetail)()

 

Dim detail1 As New OrderDetail()

detail1.Item = New Product()

detail1.Item.ProductName = "Wizard"

detail1.Item.Cost = 29.98D

detail1.Quantity = 2

detail1.Order = order

order.OrderDetails.Add(detail1)

 

Dim detail2 As New OrderDetail()

detail2.Item = New Product()

detail2.Item.ProductName = ".NET Caching Library"

detail2.Item.Cost = 199.95D

detail2.Quantity = 1

detail2.Order = order

order.OrderDetails.Add(detail2)

 

'Save parents and children

db.SaveWithChildren(order)

 

'Reload the entire tree

Dim orderCopy As Order = db.LoadWithChildren(Of Order)(order.OrderId)