学习008-02-01-04 Supply Initial Data(提供初始数据)

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:
在继续之前,请花点时间回顾一下之前的课程:

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.
您还可以使用数据播种来指定初始数据。

Next Lesson(下一课)

Configure a One-to-Many Relationship
配置一对多关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值