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 == ) {
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数据模型提供初始数据的更多相关文章
- [转]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 ...
- 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 ...
- 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用
上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...
- 第二篇:Entity Framework CodeFirst & Model 映射
前一篇 第一篇:Entity Framework 简介 我有讲到,ORM 最关键的 Mapping,也提到了最早实现Mapping的技术,就是 特性 + 反射,那Entity Framework 实现 ...
- Entity Framework 5.0系列之EF概览
概述 在开发面向数据的软件时我们常常为了解决业务问题实体.关系和逻辑构建模型而费尽心机,ORM的产生为我们提供了一种优雅的解决方案.ADO.NET Entity Framework是.NET开发中一种 ...
- [Programming Entity Framework] 第3章 查询实体数据模型(EDM)(一)
http://www.cnblogs.com/sansi/archive/2012/10/18/2729337.html Programming Entity Framework 第二版翻译索引 你可 ...
- 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...
- Entity Framework 5.0系列之EF概览-三种编程方式
概述 在开发面向数据的软件时我们常常为了解决业务问题实体.关系和逻辑构建模型而费尽心机,ORM的产生为我们提供了一种优雅的解决方案.ADO.NET Entity Framework是.NET开发中一种 ...
- Entity Framework : The model backing the '' context has changed since the database was created
1.采用code first 做项目时,数据库已经生成,后期修改数据库表结构.再次运行时出现一下问题: Entity Framework : The model backing the '' cont ...
随机推荐
- luogu P1768 天路 |01分数规划+负环
题目描述 言归正传,小X的梦中,他在西藏开了一家大型旅游公司,现在,他要为西藏的各个景点设计一组铁路线.但是,小X发现,来旅游的游客都很挑剔,他们乘火车在各个景点间游览,景点的趣味当然是不用说啦,关键 ...
- UITableView 相关方法
最近闲来无事,总结一下 UITableViewDataSource和 UITableViewDelegate方法 UITableViewDataSource @required - (NSIntege ...
- 2019CCPC秦皇岛D题 Decimal
Decimal Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- 2017 ACM/ICPC 沈阳 K题 Rabbits
Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a ...
- AI Boot Camp 分享之 ML.NET 机器学习指南
今天在中国七城联动,全球134场的AI BootCamp胜利落幕,广州由卢建晖老师组织,我参与分享了一个主题<ML.NET 机器学习指南和Azure Kinect .NET SDK概要>, ...
- Python学习笔记—自动化部署【新手必学】
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨 目录 pexpect fabric pexpect P ...
- Bayer图像处理 raw 数据解析
Bayer是相机内部的原始图片, 一般后缀名为.raw. 很多软件都可以查看, 比如PS. 我们相机拍照下来存储在存储卡上的.jpeg或其它格式的图片, 都是从.raw格式转化 过来的. .raw格式 ...
- Orleans[NET Core 3.1] 学习笔记(三)( 3 )服务端配置
服务端配置 Silo通过SiloHostBuilder和许多补充选项类以编程方式进行配置. Silo配置有几个关键方面: Orleans集群信息 集群提供程序(不知道咋翻译) Silo到Silo和Cl ...
- H5中被废弃的标签
<br>换行,已经被<p>标签进行替换 <hr>画线 <font> <b>,<u>,<i>,<s>:加粗 ...
- hikari连接池属性详解
hikari连接池属性详解 一.主要配置 1.dataSourceClassName 这是DataSourceJDBC驱动程序提供的类的名称.请查阅您的特定JDBC驱动程序的文档以获取此类名称,或参阅 ...