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. webpack-优化阻塞的css

    随着浏览器的日新月异,网页的性能和速度越来越好,并且对于用户体验来说也越来越重要. 现在有很多优化页面的办法,比如:静态资源的合并和压缩,code splitting,DNS预读取等等. 本文介绍的是 ...

  2. Linux内核最顶层文档

    Linux 内核文档 该文件是 Linux 内核文档树中最顶层的,会随着内核一起更新:其目的是把散乱的文档集成为一个逻辑清晰的完整版,非常欢迎改善文档,如果想做出自己的贡献,加入vger.kernel ...

  3. Java修炼——反射机制

    反射机制常见的作用: 1) 动态的加载类.动态的获取类的信息(属性,方法,构造器) 2) 动态构造对象 3) 动态调用类和对象的任意方法.构造器 4) 动态调用和处理属性 5) 获取泛型信息 6) 处 ...

  4. linux mysql,tomcat与java的安装

    先将服务器的安全组设置为 把所有端口或者所需要的端口开放 然后测试远程访问 ssh 用户@ip                       登录 输入密码 service iptables stop  ...

  5. io流函数略解(java_input流)[二]

    背景 在写这篇随笔之前,已经写了io流函数略解(java_File)(一),主要是总结了File的一些操作,以及一些源码介绍. 在Io实际应用中,实际上运用在如果会操作File,实际上很难写出一点能实 ...

  6. jenkins持续集成工作原理、功能、部署方式等介绍

    超详细的jenkins持续集成工作原理.功能.部署方式等介绍 原创 波波说运维 2019-08-29 00:01:00 概述 今天简单整理了一下jenkins的一些概念性内容,归纳如下: 1.概念 j ...

  7. JS系列:js数据类型的转换

    数据类型的转换[基本数据类型]数字 number字符串 string布尔 boolean空 null未定义 undefined[引用数据类型]对象 object普通对象 {}数组对象 [](Array ...

  8. WebSocket重连实现

    方式一.使用第三方库实现 比如:reconnecting-websocket.jsReconnectingWebSocket,代码:https://github.com/joewalnes/recon ...

  9. django基础之day08,分页器从无到有,动态思路解析全过程

    *********分页器从无到有的全过程,动态思路解析如下:******** 1.通过book_queryset = models.Book.objects.all()[start_num:end_n ...

  10. Django day03之学习知识点

    今日是路由层学习: 3.路由匹配 3.1 正则表达式的特点: 一旦正则表达式能够匹配到内容,会立刻结束匹配关系 直接执行对应的函数.相当于采用就近原则,一旦找到就不再继续往下走了 重点: 正则表达式开 ...