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

How to: Supply Initial Data for the Entity Framework Data Model

  • 3 minutes to read

After you have introduced a data model, you may need to have the application populate the database with a predefined set of objects. In this topic, you will learn how to add data to the database in code when the application runs. For this purpose, the code that creates an Employee object with the associated Task is demonstrated here.

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e4375/how-to-use-the-entity-framework-code-first-in-xaf.

In this example, it is assumed that you have created an XAF solution with an Entity Framework data model in accordance with the How to: Use the Entity Framework Code First in XAF or How to: Use the Entity Framework Model First in XAF instructions.

Implement the Module Updater

Open the Updater.cs (Updater.vb) file located in the Database Update folder of the MySolution.Module project, and override the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method as shown below.

public class Updater : ModuleUpdater {
    public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { }
    public override void UpdateDatabaseAfterUpdateSchema() {
        if (ObjectSpace.GetObjects<Employee>().Count == 0) {
            var employee = ObjectSpace.CreateObject<Employee>();
            employee.FirstName = "Mary";
            employee.LastName = "Tellitson";
            var task = ObjectSpace.CreateObject<Task>();
            task.Subject = "Check reports";
            task.AssignedTo = employee;
        }
        ObjectSpace.CommitChanges();
    }
}

In the code above, an Employee object with an associated Task is created if there are no Employee records in the database. As you can see, XAF uses an Object Space object to manipulate persistent objects (see Create, Read, Update and Delete Data).

Note

To learn more about updating the application database, refer to the Create and Update the Application’s Database topic.

Add the ModuleInfo Entity

The ModuleInfo entity mapped to the ModuleInfo table is used when the XafApplication.CheckCompatibilityType property is set to ModuleInfo, to store the version information of the application modules. When a module assembly version is incremented, XAF compares the actual module versions with versions stored in the database. If versions differ, the database must be updated. To support the database update, an entity that implements the data model must have the IModuleInfo interface.

Note

If you use the Solution Wizard to create an XAF solution, the ModuleInfo entity is added automatically by default.

Code First

XAF provides a built-in IModuleInfo implementor for Code First: the ModuleInfo entity. If you use Code First, register this entity within your DbContext descendant.

public class MyDbContext : DbContext {
    // ...
    public DbSet<DevExpress.ExpressApp.EF.Updating.ModuleInfo> ModuleInfo { get; set; }
}

Model First

If you use Model First, add the following entity in the designer.

EF_ModuleInfo

In the ModuleInfo.cs (ModuleInfo.vb) file, specify that the ModuleInfo entity supports the IModuleInfo interface. To hide ModuleInfo from the UI, apply the Browsable attribute and pass false as the parameter.

[Browsable(false)]
public partial class ModuleInfo : IModuleInfo {
}

After applying the changes above, the Employee and Task records will be created in the application database.