Skip to main content
All docs
V23.2

Supply Initial Data

  • 2 minutes to read

This lesson explains how to specify initial data in your application.

Note

Before you proceed, take a moment to review the previous lessons:

Step-by-Step Instructions

  1. Expand the MySolution.Module project in the Solution Explorer and go to the DatabaseUpdate folder. Open the Updater.cs file and add the following code to the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method:

    using MySolution.Module.BusinessObjects;
    //...
    
    public class Updater : DevExpress.ExpressApp.Updating.ModuleUpdater {
        //...
        public override void UpdateDatabaseAfterUpdateSchema() {
            base.UpdateDatabaseAfterUpdateSchema();
    
            Employee employeeMary = ObjectSpace.FirstOrDefault<Employee>(x => x.FirstName == "Mary" && x.LastName == "Tellitson");
            if(employeeMary == null) {
                employeeMary = ObjectSpace.CreateObject<Employee>();
                employeeMary.FirstName = "Mary";
                employeeMary.LastName = "Tellitson";
                employeeMary.Email = "tellitson@example.com";
                employeeMary.Birthday = new DateTime(1980, 11, 27);
            }
    
            ObjectSpace.CommitChanges(); //Uncomment this line to persist created object(s).
        }
    }
    

    XAF uses an Object Space instance to manipulate persistent objects (see Create, Read, Update and Delete Data).

    In the sample code above, the BaseObjectSpace.FirstOrDefault method checks the Employee list for an entry that matches the name “Mary Tellitson”. If such employee doesn’t exist, the BaseObjectSpace.CreateObject method creates it and adds the corresponding entry to the database.

  2. Run the application.

    XAF compares the application version with the database version. If the database version is older than the application version, then the application raises the DatabaseVersionMismatch event. Each platform-specific project in your solution handles this event in the descendant of the XafApplication class: BlazorApplication class (ASP.NET Core Blazor) and WinApplication class (Windows Forms).

    The application creates a database if the database does not exist. After that, the application calls the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method. The method saves the required objects to the database.

    For additional information on how database updates work, refer to the following topic: Create and Update the Application's Database.

  3. Select the Employee item in the navigation control and invoke the Detail View for the new employee:

    ASP.NET Core Blazor
    ASP.NET Core Blazor supply initial data
    Windows Forms
    Windows Forms supply initial data

Tip

You can also use Data Seeding to specify initial data.

Next Lesson

Configure a One-to-Many Relationship

See Also