How to: Delete Objects
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In order to delete an object from the database the following steps should be performed:
- You need a reference to the target object.
- Pass the object to the OpenAccessContext's Delete method.
- Invoke the SaveChanges method.
For example:
Customer customerToDelete = dbContext.Customers.Last();
dbContext.Delete( customerToDelete );
dbContext.SaveChanges();
Dim customerToDelete As Customer = dbContext.Customers.Last()
dbContext.Delete(customerToDelete)
dbContext.SaveChanges()
To remove an existing relation between objects in one-to-many relationship, simply use the object.Collection.Remove method. For example: category.Cars.Remove(car). To get this code working you need to set the IsManaged property of the collection in Visual Designer to True. For more information, read How to: Manage One-to-Many Association.
How to: Perform Cascading Delete
This section demonstrates how to automatically delete objects which are in parent-child relations using Telerik Data Access. When you want to automatically delete all of the child records of a parent record when the parent record is deleted, you need to perform a cascading delete.
Suppose, you have the following two tables in the database:
The Products table has a reference to the Categories table. Usually, the referenced table is called the parent table while the table with the foreign key is called the child table. In this example the Products table is the child table, and the Categories table is the parent table. A foreign key with a cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will be deleted automatically. This is called a cascade delete.
Configuring Your Domain Model to Support Cascading Delete
Suppose, you have created a domain model reflecting the Categories and Products tables.
Cascading deletes are disabled by default and are only enabled for references specified as dependent. In order to configure your domain model to support cascading delete, you need to perform few simple steps:
- Locate the parent entity in the Visual Designer (in this example, this is the Category entity) and select the navigational property that will be specified as dependent (in this example, this is the Products property).
- Press F4 to open the Properties pane.
- In Properties pane, set the IsDependent property to True.
In this example the Products property is specified as dependent. When you delete a specific Category object, then all referenced Product objects are deleted, too.
Telerik Data Access does not check for other references to the deleted dependent instances. If there are other references to the deleted dependent instances, you will get an exception. For example, consider the following domain model.
The Products property of the Category class is configured as dependent. At the same time, the OrderDetails property of the Product entity is not configured as dependent. If you delete a specific Category object, Telerik Data Access will try to delete all related Products. However, if there are OrderDetail objects referencing any of the deleted dependent products, you will get an exception. The correct configuration here is to specify the OrderDetails property as dependent.
Thus, when you delete a Category object, Telerik Data Access will delete all related Product objects, and all OrderDetail objects related to the deleted Product.
One additional thing you should know about is that the cascade delete does not work in the "parent->child" direction only. You could configure your domain model so that the cascade delete is available in the opposite direction, too. For example, if you make the Category property of the Product entity dependent, then you will achieve the following result: when you delete a child entity (e.g. Product), the corresponding parent entity (e.g. Category) will be deleted too.