Knight Data Access Layer
Using IsDeleted
Basic Tasks C# > Deleting Data > Using IsDeleted

When there is an IsDeleted property defined on a class and it is true, the object will be deleted when the Save is called.

 

[TableMapping("OrderDetails")]

public class OrderDetail

{

[PrimaryKey(PrimaryKeyType.Identity)]

public int OrderDetailId { get; set; }

 

[ParentObject("OrderId")]

public Order Order { get; set; }

 

public string Notes { get; set; }

public bool IsDirty { get; set; }

public bool IsDeleted { get; set; }

}

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

Order order = new Order();

List<OrderDetail> orderDetails =

new List<OrderDetail>

{

new OrderDetail

{

Notes = "Order detail 1",

Order = order

},

new OrderDetail

{

Notes = "Order detail 2",

Order = order

},

new OrderDetail

{

Notes = "Order detail 3",

Order = order

}

};

 

order.OrderDetails = orderDetails;

 

// Insert records

_dataHelper.SaveWithChildren(orderDetails);

 

orderDetails[1].Notes = "Updated order detail 2";

orderDetails[1].IsDirty = true;

orderDetails[1].IsDeleted = false;

 

// Update second order detail

_dataHelper.Save(orderDetails);

 

orderDetails[1].IsDirty = false;

orderDetails[1].IsDeleted = true;

 

// Delete second order detail

_dataHelper.Save(orderDetails);