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. Java学习笔记(面向对象上)

    面向对象(上) 面向对象的特点:封装性(是面向对象的核心思想,将对象的属性和行为封装起来).继承性(主要描述类与类之间的关系,通过继承,可以在无需重新编写原有类的情况下,对原有类的功能进行扩展)和多态 ...

  2. Java并发编程学习随笔 (一) 使用run() 和 start()的差别

    java多线程run()和start()的区别 当你启动线程,使用start(),系统会把run()方法当成线程执行体来处理,这是正常的,也是正确的情况.但是,当你启动线程时,调用run()方法,系统 ...

  3. openlayers6结合geoserver实现地图空间查询(附源码下载)

    前言 之前写过一篇 openlayers4 版本的地图空间查询文章,但是由于是封装一层 js 代码写的,很多初学者看起来比较有点吃力,所以本篇文章重新写一篇地图空间查询文章,直接基于最新版本 open ...

  4. A.Changing Volume

    题目:改变音量 题意:给定两个数a和b,有6个操作(-5, -2, -1, +1, +2, +5),求a变到b的最小操作次数 操作的过程中不能变到小于0,即音量不能调到小于0 分析: (贪心),我们可 ...

  5. TVP5150 PAL/NTSC ccd cmos 模拟摄像头视频 转换 VGA输出

    新做了一批 视频模块,特此总结下: 解码ic:tvp5150 输入:CVBS  PAL 输出:DVP(bt565  yuv  dvp8  含vs hs) 测试分辨力:(vga输出 640*480) 实 ...

  6. MVC模式与Servlet执行流程

    ##Servlet生命周期 五个部分,从加载到卸载,如同人类的出生到死亡 加载:Servlet容器自动处理 初始化:init方法 该方法会在Servlet被加载并实例化后执行 服务:service抽象 ...

  7. Python基础-day01-4

    多文件项目的演练 开发 项目 就是开发一个 专门解决一个复杂业务功能的软件 通常每 一个项目 就具有一个 独立专属的目录,用于保存 所有和项目相关的文件 一个项目通常会包含 很多源文件 目标 在项目中 ...

  8. Wonder发布v1.1正式版本,新增资产预览、脚本、AssetBundle等

    更新说明 本次版本重点增加了脚本组件,并且实现了类似于unity的AssetBundle,支持动态加载场景和资源. 相关链接 官网 Wonder官方QQ群: 106047770 相关资料 Wonder ...

  9. 图解leetcode —— 128. 最长连续序列

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...

  10. .netcore实现一个读写分离的数据库访问中间件

    在实际业务系统中,当单个数据库不能承载负载压力的时候,一般我们采用数据库读写分离的方式来分担数据库负载.主库承担写以及事务操作,从库承担读操作. 为了支持多种数据库我们先定义一个数据类型字典.key为 ...