Supply Initial Data (XPO)
- 3 minutes to read
Tip
For .NET 6 applications, see: Supply Initial Data (XPO, .NET 6).
Note
Before proceeding, take a moment to review the Inherit from the Business Class Library Class 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.FirstOrDefault<Contact>(c => c.FirstName == "Mary" && c.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 Web Forms 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 Web Forms application. Select the Contact item in the navigation control. Notice that the new contact, “Mary Tellitson”, appears in the list to the right.
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 22.1\Components\XAF\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.
Next Lesson: Implement Custom Business Classes and Reference Properties