Skip to main content
A newer version of this page is available.

Supply Initial Data (XPO)

  • 3 minutes to read

Note

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

  • Open the Updater.cs (Updater.vb) 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("FirstName == 'Mary' && 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();
        }
    }
    

    After adding the code above, the Contact object will be created in the application database, if it does not already exist.

    Each time you run the application, it compares the application version with the database version and finds changes in the application or database. If the database version is lower than the application version, the application raises the XafApplication.DatabaseVersionMismatch event. This event is handled by the WinForms and ASP.NET applications 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.

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

    To specify the criteria passed as a parameter in the BaseObjectSpace.FindObject method call, the CriteriaOperator is used. Its CriteriaOperator.Parse method converts a string, specifying a criteria expression to its CriteriaOperator equivalent. To learn more on how to specify criteria, refer to the Ways to Build Criteria topic.

  • Run the WinForms or ASP.NET application. Select the Contact item in the navigation control. Notice that the new contact, “Mary Tellitson”, appears in the list to the right.

    Tutorial_BMD_Lesson2_5_1

You can see the code for this tutorial in the MySolution.Module | Database Update | Updater.cs (Updater.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 19.1\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version of this demo is available online at https://demos.devexpress.com/XAF/MainDemo.

 

Next Lesson: Implement Custom Business Classes and Reference Properties (XPO)

See Also