Skip to main content
All docs
V21.2
.NET Framework 4.5.2+

Supply Initial Data (EF 6)

  • 3 minutes to read

Note

Before proceeding, take a moment to review the Inherit from the Business Class Library Class (EF 6) 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.

    Tutorial_BMD_Lesson2_5_1

Note

Note that in the Inherit from the Business Class Library Class (EF 6) lesson, the database initializer was set to clear the database in case the business model changes. This means that all objects created at runtime will be deleted after the next change in the business model. It is recommended that you use the approach described in this lesson to create all objects required for testing purposes. They will persist in your application regardless of the database.

You can see the code for this tutorial in the EFDemo.Module | Database Update | Updater.cs (Updater.vb) file of the EF Demo (Code First) demo installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 21.2\Components\eXpressApp Framework\EFDemoCodeFirst.

 

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