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

如果使用"模型优先",则在设计器中添加以下实体。

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.

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

How to: Supply Initial Data for the Entity Framework Data Model 如何:为EF数据模型提供初始数据的更多相关文章

  1. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

  2. How to: Use the Entity Framework Data Model Located in an External Assembly 如何:使用位于外部程序集中的EF数据模型

    If you have a non-XAF application, and want to develop an XAF application that utilizes the same dat ...

  3. 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用

    上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...

  4. 第二篇:Entity Framework CodeFirst & Model 映射

    前一篇 第一篇:Entity Framework 简介 我有讲到,ORM 最关键的 Mapping,也提到了最早实现Mapping的技术,就是 特性 + 反射,那Entity Framework 实现 ...

  5. Entity Framework 5.0系列之EF概览

    概述 在开发面向数据的软件时我们常常为了解决业务问题实体.关系和逻辑构建模型而费尽心机,ORM的产生为我们提供了一种优雅的解决方案.ADO.NET Entity Framework是.NET开发中一种 ...

  6. [Programming Entity Framework] 第3章 查询实体数据模型(EDM)(一)

    http://www.cnblogs.com/sansi/archive/2012/10/18/2729337.html Programming Entity Framework 第二版翻译索引 你可 ...

  7. 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...

  8. Entity Framework 5.0系列之EF概览-三种编程方式

    概述 在开发面向数据的软件时我们常常为了解决业务问题实体.关系和逻辑构建模型而费尽心机,ORM的产生为我们提供了一种优雅的解决方案.ADO.NET Entity Framework是.NET开发中一种 ...

  9. Entity Framework : The model backing the '' context has changed since the database was created

    1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the '' cont ...

随机推荐

  1. 第八次作业-非确定的自动机NFA确定化为DFA

    NFA 确定化为 DFA 子集法: f(q,a)={q1,q2,…,qn},状态集的子集 将{q1,q2,…,qn}看做一个状态A,去记录NFA读入输入符号之后可能达到的所有状态的集合. 步骤: 1. ...

  2. 在Eclipse中混合Java和Scala编程

    1. 新建项目目录 scala-java-mix 2. 创建 src 目录及子目录: mkdir -p src/main/java mkdir -p src/main/scala 3. 在目录 sca ...

  3. react-native android/ios 手动/自动 修改版本号

    目录 一 手动操作 1.Android 2.IOS 二 自动从配置文件读取并写入相应的地方 1.package.json配置版本 2.Android: 3.IOS 添加sell代码 当版本迭代时候要修 ...

  4. [TimLinux] django context_processor介绍

    1. context django里面 render 函数,HttpResponse,都有一个参数,context={},这个参数用于将视图层处理得到的数据传递到模板层. 2. context_pro ...

  5. UVA11324 The Lagest Lique(SCC缩点+DP)

    Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to ...

  6. [FPGA]Verilog 60s秒表计时器(最大可计时间长达9min)

    [FPGA]Verilog 60s秒表计时器 1.引述 这次的实验来自于本人本科课程数电结课时的自选题目.由于这次上传是后知后觉,学校已将小脚丫板子回收,所以在这篇文章中没法贴出代码结果的效果图了,但 ...

  7. iOS开发 为何 大不如前?原因竟然是这个?

    前言: 近期,社会上对iOS开发行业的负面信息越来越多,并且还被一些黑骗机构的胡诌八扯越描越黑,现在iOS开发按照开发者与公司招聘的数量上来看,是显示的供大于求,但是从各公司的招人计划上来看,明显是供 ...

  8. 利用Bootstrap搭建网站页面

    先来看下页面效果 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. 阿里最新38道Java面试题解析(MyBatis+消息队列+Redis)

    一.谈谈你对 MyBatis 的理解? 1. Mybatis是一个半ORM(对象关系映射)框架,它内部封装了 JDBC,开发时只需要关注 SQL 语句本身,不需要花费精力去处理加载驱动.创建连接.创建 ...

  10. poj 3279 Fliptile (简单搜索)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Descrip ...