All docs
V20.2
20.2
20.1
The page you are viewing does not exist in version 20.1. This link will take you to the root page.
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Supply Initial Data (XPO)

  • 2 minutes to read
NOTE

Before proceeding, take a moment to review the Inherit from the Business Class Library Class (XPO) lesson.

Step-by-Step Instructions

  1. Open the Updater.cs file located in the MySolution.Module project's Database Update folder. 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();
    
            Contact contactMary = ObjectSpace.FindObject<Contact>(
                CriteriaOperator.Parse($"{nameof(Contact.FirstName)} == 'Mary' && {nameof(Contact.LastName)} == 'Tellitson'");
            if (contactMary == null) {
                contactMary = ObjectSpace.CreateObject<Contact>();
                contactMary.FirstName = "Mary";
                contactMary.LastName = "Tellitson";
                contactMary.Email = "tellitson@example.com";
                contactMary.Birthday = new DateTime(1980, 11, 27);
            }
            //...
            ObjectSpace.CommitChanges();
        }
    }
    
  2. Run the application. Select the Contact item in the navigation control. Notice that the new contact, "Mary Tellitson", appears on the list to the right.

    xaf blazor supply initial data

Detailed Explanation

Database Update

In step 1, you add a code that checks whether the contact list contains an entry that matches the name "Mary Tellitson". If such a contact does not exist, the code creates and adds it to the database.

An XAF application checks application and database version compatibility on each start. The application compares module versions stored in the database with actual module versions. If the database version is lower than the application version, the application raises the XafApplication.DatabaseVersionMismatch event.

The Blazor application handles this event in a solution template. When the application runs in debug mode, this event handler uses the built-in Database Updater to update the application's database. After the database schema is updated, the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method is called. In this method, you can save the required business objects to the database.

Query Data

As you can see in the code above, XAF uses an Object Space object to manipulate persistent objects (see Create, Read, Update and Delete Data).

The sample uses a CriteriaOperator to specify the parameter in the BaseObjectSpace.FindObject method call. The CriteriaOperator.Parse method accepts string expressions. For more ways to specify criteria, refer to the Ways to Build Criteria topic.

Next Lesson

Implement Custom Business Classes and Reference Properties (XPO)

See Also