M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Database.Delete()

In this lesson, we will learn about possible ways to delete a record from the database. M# provides Database.Delete method for this purpose, which requires an entity instance to be deleted from database.

M# provides three overloads of Database.Delete method. In this lesson, we will work with the Employee entity and will see how we can call Database.Delete on an entity instance(s).

Note: In the screenshots below, the keyword this represents the current instance of an Employee entity.

Soft Deleting

M# allows developers to soft delete records in the database. You can mark an entity as "Soft Delete" in M# and calling Database.Delete method on the entity instances updates the ".Deleted" special column on the record as "True", rather than deleting the record permanently.

Setting an Entity as Soft Delete

You can mark an entity as Soft Delete by selecting "Soft Delete" attribute on the Entity in M# Model Management Section, as shown below:

For more information on entities in M#, please read lesson Entity, Page, Module in chapter 1

M# updates the entity class and marks the class with [SoftDelete] attribute, as in our case the Employee entity class is marked as "Soft Delete", as shown below:

M# creates a new column on the SQL Table shown below to manage soft delete records.

Overloads - 1 Delete Single Employee Record

Example

Overloads - 2 Delete Collection of Employee Records

Example

Overloads - 3 Delete Single Employee with Delete behaviour

Example

The code above shows that we are deleting a record of Employee,  also supplying an additional parameter "DeleteBehaviour". The additional parameter is used in special cases when you want to bypass any event in the workflow of the Database.Delete method. DeleteBehaviour enum and the events raised by M# while deleting an instance are discussed below.

Events Raised During Database.Delete

Delete Behaviour Enum

M# provides Database.Delete method overload with an extra parameter of an enum type DeleteBehaviour. This enum type is used to bypass the events raised while deleting an entity instance as required in the business logic, the enum options are shown below: