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.Save()

In this lesson, we will learn about possible ways to persist an entity instance in the database. M# offers Databas.Save() method for this purpose. M#'s provided data persistence facility is not only limited to single entity instance but is also available for persisting a collection of records.

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

Note: Database. Save also returns the saved instance(s) to be used later in code.

Overloads

Examples

Overload 1 – Saving a New Employee Instance

The picture above demonstrates that we are saving a new instance of Employee entity.

Overload 2 – Saving an Array of Employees

M# runs bulk saves in “Transaction”. Meaning, failure to save one records results rolling back all the saved instances of the current collection

Overload 5 – Saving a Single Employee by Specifying Save Behaviour

The code above shows that we are saving a new record of Employee, but also supplying an additional parameter “SaveBehaviour”. The additional parameter is used in special cases when you want to omit any event in the pipeline of the Database.Save method. SaveBehaviour enum and save pipeline is discussed later in this chapter.

Overload 6 – Saving a List of Employees by Specifying Save Behaviour

The code shown above is saving a List of Employees by specifying “SaveBehaviour”.

Important: Database.Save can also be used to update an existing record. Although, it is not recommended to call Database.Save method for updating an existing entity instance, but in exceptional cases, always “Clone” the entity instance before changing and then call Database.Save. by passing cloned object. The reason is that you are changing the object that is already referenced on data cache so If Save action fails the object on cache would be different than the one in database. (Memory Cache is not transactional). M# offers another method “Database.Update” to update existing records, which should be used always and is discussed in next lesson.

Events Raised During Database.Save

Whenever an entity instance is saved or updated, it goes through a series of events, which are used to implement core business logic e.g. validation. M# allows developers to override these events in the Logic partial class of an entity.

For more information on Partial classes, please read lesson Partial Classes and Business Logic in chapter 2.

Below are the events overridden in Logic class of an entity



Save Behaviour Enum

M# provides Database.Save method overloads with an extra parameter of an enum type SaveBehaviour. This enum type is used to bypass the events raised (Shown above ) while saving an entity instance as required in the business logic, below is the snapshot of the enum options

For example, if you want to supress the onSaved event of an entity when you save an entity instance then you should use the SaveBehaviour.BypassSaved enum option, as shown in below screenshot