All docs
V21.2
21.2
21.1
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.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+
.NET 5.0+

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.FirstOrDefault<Contact>(contact => contact.FirstName == "Mary" && 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 ASP.NET Core 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 following checks are performed:

  • The database exists.
  • All required tables exist.
  • All required columns exist.

The XafApplication.DatabaseVersionMismatch event occurs if any of these checks fails.

The ASP.NET Core 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).

Next Lesson

Implement Custom Business Classes and Reference Properties (XPO)

See Also