Supply Initial Data(提供初始数据)
This lesson explains how to specify initial data in your application.
本课介绍如何在应用程序中指定初始数据。
Note(注)
Before you proceed, take a moment to review the previous lessons:
在继续之前,请花点时间回顾一下之前的课程:
- Implement a Data Model: Basics(实施数据模型:基础)
- Extend the Data Model(扩展数据模型)
Step-by-Step Instructions(分步说明)
1.Expand the MySolution.Module project in the Solution Explorer and go to the DatabaseUpdate folder. Open the Updater.cs file and add the following code to the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method:
在解决方案资源管理器中展开MySolutions. Module项目并转到Database aseUpdate文件夹。打开Updater.cs文件并将以下代码添加到ModuleUpdater.UpdateDatabaseAfterUpdateSchema方法:
C#
using MySolution.Module.BusinessObjects;
//...
public class Updater : DevExpress.ExpressApp.Updating.ModuleUpdater {
//...
public override void UpdateDatabaseAfterUpdateSchema() {
base.UpdateDatabaseAfterUpdateSchema();
Employee employeeMary = ObjectSpace.FirstOrDefault<Employee>(x => x.FirstName == "Mary" && x.LastName == "Tellitson");
if(employeeMary == null) {
employeeMary = ObjectSpace.CreateObject<Employee>();
employeeMary.FirstName = "Mary";
employeeMary.LastName = "Tellitson";
employeeMary.Email = "tellitson@example.com";
employeeMary.Birthday = new DateTime(1980, 11, 27);
}
ObjectSpace.CommitChanges(); //Uncomment this line to persist created object(s).
}
}
XAF uses an Object Space instance to manipulate persistent objects (see Create, Read, Update and Delete Data).
XAF使用对象空间实例来操作持久对象(请参阅创建、读取、更新和删除数据)。
In the sample code above, the BaseObjectSpace.FirstOrDefault method checks the Employee list for an entry that matches the name “Mary Tellitson”. If such employee doesn’t exist, the BaseObjectSpace.CreateObject method creates it and adds the corresponding entry to the database.
在上面的示例代码中,BaseObjectSpace. FirstOrDefault方法会检查员工列表中与名称“Mary Tellitson”匹配的条目。如果该员工不存在,则BaseObjectSpace.CreateObject方法会创建它并将相应的条目添加到数据库中。
2.Run the application.
运行应用程序。
XAF compares the application version with the database version. If the database version is older than the application version, then the application raises the DatabaseVersionMismatch event. Each platform-specific project in your solution handles this event in the descendant of the XafApplication class: BlazorApplication class (ASP.NET Core Blazor) and WinApplication class (Windows Forms).
XAF将应用程序版本与数据库版本进行比较。如果数据库版本早于应用程序版本,则应用程序会引发DatabaseVersionMismatch事件。解决方案中的每个特定于平台的项目都会在XafApplication类的后代中处理此事件:BlazorApplication类(ASP.NETCore Blazor)和WinApplication类(Windows窗体)。
The application creates a database if the database does not exist. After that, the application calls the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method. The method saves the required objects to the database.
如果数据库不存在,应用程序将创建一个数据库。之后,应用程序调用ModuleUpdater.UpdateDatabaseAfterUpdateSchema方法。该方法将所需的对象保存到数据库中。
For additional information on how database updates work, refer to the following topic: Create and Update the Application’s Database.
有关数据库更新如何工作的更多信息,请参阅以下主题:创建和更新应用程序的数据库。
3.Select the Employee item in the navigation control and invoke the Detail View for the new employee:
在导航控件中选择员工项并为新员工调用详细信息视图:
ASP.NET Core Blazor
Windows Forms
Tip
You can also use Data Seeding to specify initial data.
您还可以使用数据播种来指定初始数据。