How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据

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 http://www.devexpress.com/example=E4375
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E4375

.

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.

在此示例中,假定您已根据"如何:在 XAF 中首先使用实体框架代码"或"如何:在 XAF 指令中首先使用实体框架模型"创建了具有实体框架数据模型的 XAF 解决方案。

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.

打开位于 MySolution.模块项目的数据库更新文件夹中的Updater.cs(Updater.vb)文件,然后重写模块更新器.更新数据库后更新架构方法,如下所示。

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).

在上面的代码中,如果数据库中没有员工记录,则创建具有关联任务的员工对象。如您所见,XAF 使用对象空间对象操作持久对象(请参阅创建、读取、更新和删除数据)。

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.

当 XafApplication.Check兼容性类型属性设置为 ModuleInfo 时,将使用映射到模块信息表的模块信息实体来存储应用程序模块的版本信息。当模块程序集版本递增时,XAF 会将实际模块版本与数据库中存储的版本进行比较。如果版本不同,则必须更新数据库。为了支持数据库更新,实现数据模型的实体必须具有 IModuleInfo 接口。

Note 注意
If you use the Solution Wizard to create an XAF solution, the ModuleInfo entity is added automatically by default.
如果使用解决方案向导创建 XAF 解决方案,默认情况下将自动添加 ModuleInfo 实体。

 

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.

XAF 为代码优先:模块信息实体提供了内置的 IModuleInfo 实现器。如果使用代码优先,请在 DbContext 后代中注册此实体。

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.

在ModuleInfo.cs (ModuleInfo.vb) 文件中,指定 ModuleInfo 实体支持 IModuleInfo 接口。要从 UI 中隐藏 ModuleInfo,请应用"可浏览"属性并将 false 传递为参数。

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

 

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

应用上述更改后,将在应用程序数据库中创建"员工"和"任务"记录。

posted @ 2019-12-18 16:34  code first life  阅读(137)  评论(0编辑  收藏  举报