Knight Data Access Layer
Deleting with Children
Basic Tasks C# > Deleting Data > Deleting with Children

Delete an object and all associated children in the database.

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

//Build a record to save to the database

Order order = new Order();

order.Buyer = new Person();

order.Buyer.FirstName = "John";

order.Buyer.LastName = "Smith";

order.Buyer.DateCreated = DateTime.Now;

order.OrderDetails = new List<OrderDetail>();

 

OrderDetail detail1 = new OrderDetail();

detail1.Item = new Product();

detail1.Item.ProductName = "Wizard";

detail1.Item.Cost = 29.98M;

detail1.Quantity = 2;

detail1.Order = order;

order.OrderDetails.Add(detail1);

 

OrderDetail detail2 = new OrderDetail();

detail2.Item = new Product();

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

detail2.Item.Cost = 199.95M;

detail2.Quantity = 1;

detail2.Order = order;

order.OrderDetails.Add(detail2);

 

//Save it

db.SaveWithChildren(order);

 

//Delete it

db.DeleteWithChildren(order);