学习008-01-02 Define the Data Model and Set the Initial Data(定义数据模型并设置初始数据 )

Define the Data Model and Set the Initial Data(定义数据模型并设置初始数据 )

This topic explains how to implement entity classes for your application. It also describes the basics of automatic user interface construction based on a data model; this includes information on how to do the following:
本主题介绍如何为您的应用程序实现实体类。它还描述了基于数据模型的自动用户交互界面构建的基础知识;这包括有关如何执行以下操作的信息:

  • Create entity classes.(创建实体类。 )
  • Use migrations to propagate the data model structure changes to the database.(使用迁移将数据模型结构更改传播到数据库。 )
  • Populate the database with initial data.(用初始数据填充数据库。)

Entity classes do not depend on the application UI. Implement them in a platform-independent module project. This code architecture allows XAF and non-XAF applications to share entities (for instance, with a Web API Service and .NET MAUI, JavaScript, or Blazor clients).
实体类不依赖于应用程序UI。在platform-independent模块项目中实现它们。此代码体系结构允许XAF和非XAF应用程序共享实体(例如,使用Web API Service和. NET MAUI、JavaScript或Blazor客户端)。

The application’s data model consists of one logical block:
应用程序的数据模型由两个逻辑块组成:

  • Marketing: the Customer and Testimonial classes.
  • 市场营销:客户和推荐课程。

Add Entity Classes for the Marketing Group(为营销组添加实体类)

Go to the SimpleProjectManager.Module project and right-click the Business Objects folder. Choose Add | Class… in the context menu. Specify Customer.cs as the new class name and click Add. Replace auto-generated code with the following class declaration:
转到SimpleProjectManager. Module项目并右键单击Business Objects文件夹。在上下文菜单中选择Add|Class…。将Custore.cs指定为新的类名,然后单击Add。将自动生成的代码替换为以下类声明:

C#

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;namespace SimpleProjectManager.Module.BusinessObjects
{// Use this attribute to place a navigation item that corresponds to the entity class in the specified navigation group.[NavigationItem("Marketing")]// Inherit your entity classes from the BaseObject class to support CRUD operations for the declared objects automatically.public class Customer : BaseObject{public virtual String FirstName { get; set; }public virtual String LastName { get; set; }// Use this attribute to specify the maximum number of characters that the property's editor can contain.[FieldSize(255)]public virtual String Email { get; set; }public virtual String Company { get; set; }public virtual String Occupation { get; set; }public virtual IList<Testimonial> Testimonials { get; set; } =new ObservableCollection<Testimonial>();public String FullName{get { return ObjectFormatter.Format("{FirstName} {LastName} ({Company})",this, EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty); }}// Use this attribute to show or hide a column with the property's values in a List View.[VisibleInListView(false)]// Use this attribute to specify dimensions of an image property editor.[ImageEditor(ListViewImageEditorCustomHeight = 75, DetailViewImageEditorFixedHeight = 150)]public virtual MediaDataObject Photo { get; set; }}
}

In the same manner, create the Testimonial class. Replace the generated class declaration with the following code:
以同样的方式,创建Testimonial类。将生成的类声明替换为以下代码:

C#

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;namespace SimpleProjectManager.Module.BusinessObjects
{[NavigationItem("Marketing")]
public class Testimonial : BaseObject
{
public virtual string Quote { get; set; }[FieldSize(512)]
public virtual string Highlight { get; set; }[VisibleInListView(false)]
public virtual DateTime CreatedOn { get; set; }public virtual string Tags { get; set; }public virtual IList<Customer> Customers { get; set; } = new ObservableCollection<Customer>();}
}

The Testimonials collection property in the Customer class defines the first part of the many-to-many relationship between the Customer and Testimonial entities. The declaration of the Customers collection property in the Testimonial class completes the relationship.
客户类中的Testimonial集合属性定义了客户和Testimonial实体之间多对多关系的第一部分。Testimonial类中客户集合属性的声明完成了关系。

The most common relationship creation practice is to define properties on both ends of the relationship. At the same time, it is sufficient to add only the reference property (the “one” part). This property establishes the one-to-many relationship between entities, and Entity Framework Core automatically creates a foreign key to the related table in a database.
最常见的关系创建实践是在关系的两端定义属性。同时,只添加引用属性(“一”部分)就足够了。该属性建立实体之间的一对多关系,实体框架核心自动创建数据库中相关表的外键。

You can use either of these techniques, but when you omit the “many” part of the relationship, XAF does not create an editor for the omitted collection property in the Detail View of the entity class.
您可以使用这些技术中的任何一种,但是当您省略关系的“许多”部分时,XAF不会为实体类的详细信息视图中省略的集合属性创建编辑器。

Go to the SimpleProjectManager.Module\BusinessObjects\MySolutionDbContext file and add the following properties to the SimpleProjectManagerEFCoreDbContext entity container class:
转到SimpleProjectManager. Module\BusinessObjects\MySolutionDbContext文件并将以下属性添加到SimpleProjectManagerEFCoreDbContext实体容器类:

C#

using SimpleProjectManager.Module.BusinessObjects;namespace  SimpleProjectManager.Module.BusinessObjects {public class SimpleProjectManagerEFCoreDbContext : DbContext {//...public DbSet<Customer> Customers { get; set; }public DbSet<Testimonial> Testimonials { get; set; }}
}   

These properties store collections of entity class objects. For each collection, XAF creates a table with the same name in the database and then maps the collection to the table.
这些属性存储实体类对象的集合。对于每个集合,XAF 在数据库中创建一个同名的表,然后将该集合映射到该表。

Set up Migrations(设置迁移)

Since this tutorial uses Entity Framework Core, changes to the application’s data model may cause database-related exceptions when you run the application. An exception occurs if the database structure does not correspond to the structure of the data model classes.
由于本教程使用Entity Framework Core,因此在运行应用程序时,对应用程序数据模型的更改可能会导致与数据库相关的异常。如果数据库结构与数据模型类的结构不对应,则会发生异常。

In this tutorial, we use migrations to update the database schema because this feature is native to EF Core and is quick to implement. Every time you change the data model of your application, create a migration and update the database. To do this, follow the steps below.
在本教程中,我们使用迁移来更新数据库模式,因为此功能是EF Core的原生功能,并且可以快速实现。每次更改应用程序的数据模型时,请创建迁移并更新数据库。为此,请按照以下步骤操作。

Important(重要提醒)
Delete an existing database if there is one before you proceed, because Entity Framework Core does not take the existing database schema into consideration when it generates the first migration.
在继续之前删除现有数据库(如果有),因为Entity Framework Core在生成第一次迁移时不会考虑现有数据库模式。

1.Add the Microsoft.EntityFrameworkCore.Tools NuGet package to the SimpleProjectManager.Module project. Build the solution.
将Microsoft.EntityFrameworkCore.Tools NuGet包添加到SimpleProjectManager。模块项目。构建解决方案。

Note(注)
The package’s version must match the version of Entity Framework Core supported in XAF.
包的版本必须与XAF支持的Entity Framework Core版本匹配。
Currently, we support Entity Framework Core 8.x.x. To find out which precise version you have, check the Microsoft.EntityFrameworkCore package in the dependencies of the YourProjectName.Module project.
目前,我们支持Entity Framework Core 8. x.x。要了解您拥有的精确版本,请查看YourProjectName.Module项目依赖项中的Microsoft.EntityFrameworkCore包。

2.In the SimpleProjectManager.Module project, go to the BusinessObjects folder and open the SimpleProjectManagerDbContext.cs file. Replace the declaration of the SimpleProjectManagerDesignTimeDbContextFactory class with the code below:
在SimpleProjectManager. Module项目中,转到BusinessObjects文件夹,打开SimpleProjectManagerDbContext.cs文件。用下面的代码替换SimpleProjectManagerDesignTimeDbContextFactory类的声明:

C#

namespace SimpleProjectManager.Module.BusinessObjects;
//...public class SimpleProjectManagerDesignTimeDbContextFactory : IDesignTimeDbContextFactory<SimpleProjectManagerEFCoreDbContext> {public SimpleProjectManagerEFCoreDbContext CreateDbContext(string[] args) {// Throw new InvalidOperationException("Make sure that the database connection string and connection provider are correct. After that, uncomment the code below and remove this exception.");var optionsBuilder = new DbContextOptionsBuilder<SimpleProjectManagerEFCoreDbContext>();optionsBuilder.UseSqlServer("Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=SimpleProjectManager");// Automatically implements the INotifyPropertyChanged interface in the business objectsoptionsBuilder.UseChangeTrackingProxies();optionsBuilder.UseObjectSpaceLinkProxies();return new SimpleProjectManagerEFCoreDbContext(optionsBuilder.Options);}
}

In the above code sample, the optionsBuilder.UseChangeTrackingProxies method enables the change-tracking proxies extension so that the application’s UI correctly reflects changes in data model. Refer to the Change Tracking in EF Core DbContext and Performance Considerations article for more information on change tracking in XAF applications with Entity Framework Core data models.
在上面的代码示例中,optionsBuilder.UseChangeTrackingProxies方法启用更改跟踪代理扩展,以便应用程序的UI正确反映数据模型中的更改。有关使用Entity Framework Core数据模型的XAF应用程序中更改跟踪的更多信息,请参阅EF Core DbContext和性能注意事项中的更改跟踪文章。

3.In Visual Studio, open the Package Manager Console and use the following command to add a migration:
在Visual Studio中,打开包管理器控制台并使用以下命令添加迁移:

console

add-migration MyInitialMigrationName -StartupProject “SimpleProjectManager.Module” -Project “SimpleProjectManager.Module”

4.Update the database with the following command:
使用以下命令更新数据库:

console

update-database -StartupProject “SimpleProjectManager.Module” -Project “SimpleProjectManager.Module”

You must update the database whenever you change the data model of your application, for example, when you add, rename, or delete a class or property. To do this, repeat steps 3 and 4 of this tutorial. Make sure to use a unique migration name for each new migration.
每当您更改应用程序的数据模型时,您都必须更新数据库,例如,当您添加、重命名或删除类或属性时。为此,请重复本教程的步骤3和4。确保为每个新迁移使用唯一的迁移名称。

Populate the Database with Initial Data(用初始数据填充数据库)

Expand the SimpleProjectManager.Module project in the Solution Explorer and go to the DatabaseUpdate folder. Open the Updater.cs file and add the following code to the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method:
展开SimpleProjectManager。解决方案资源管理器中的模块项目并转到数据库更新文件夹。打开Updater. cs文件并将以下代码添加到ModuleUpdater。UpdateDatabaseAfterUpdateSchema方法:

C#

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Updating;
using SimpleProjectManager.Module.BusinessObjects;
// ...public class Updater : ModuleUpdater {//...public override void UpdateDatabaseAfterUpdateSchema() {base.UpdateDatabaseAfterUpdateSchema();Customer customer = ObjectSpace.FirstOrDefault<Customer>(c =>c.FirstName == "Ann" && c.LastName == "Devon");if (customer == null){customer = ObjectSpace.CreateObject<Customer>();customer.FirstName = "Ann";customer.LastName = "Devon";customer.Company = "Eastern Connection";}ObjectSpace.CommitChanges(); // Uncomment this line to persist created objects.}// ...
}

XAF uses an Object Space to manage persistent objects. Refer to the following topic for detailed information: Create, Read, Update and Delete Data).
XAF使用对象空间来管理持久对象。有关详细信息,请参阅以下主题:创建、读取、更新和删除数据)。

Note(注)
For more information on how to populate the database with initial data, review the following lesson of the In-Depth .NET WinForms & Blazor UI Tutorial: Supply Initial Data.
有关如何使用初始数据填充数据库的更多信息,请查看深入的以下课程。NET WinForms和Blazor UI教程:提供初始数据。

Run the Application(运行应用程序)

Click Start Debugging or press F5.
单击开始调试或按F5。

XAF generates a user interface based on the specified data structures. The navigation control contains items that correspond to the entity classes you created. List and Detail Views support CRUD operations and other functionality, such as navigation, search, filter, or print. Detail Views contain editors that display different entity class properties. For more information about built-in editors, refer to the following topic: Data Types Supported by built-in Editors.
XAF根据指定的数据结构生成用户交互界面。导航控件包含与您创建的实体类相对应的项目。列表和详细视图支持CRUD操作和其他功能,如导航、搜索、过滤或打印。详细视图包含显示不同实体类属性的编辑器。有关内置编辑器的更多信息,请参阅以下主题:内置编辑器支持的数据类型。

Lookup and collection editors display properties that constitute a relationship between entities. For example, the Testimonials group in the Customer Detail View is how XAF renders the Testimonials collection property. To create new objects in the Testimonials collection, use the New button in this tab. The Link button allows users to add references to existing Testimonial objects.
查找和集合编辑器显示构成实体之间关系的属性。例如,客户详细信息视图中的Testimonial组是XAF呈现Testimonial集合属性的方式。要在Testimonial集合中创建新对象,请使用此选项卡中的New按钮。Link按钮允许用户添加对现有Testimonial对象的引用。

ASP.NET Core Blazor
在这里插入图片描述
Windows Forms
在这里插入图片描述
Note(注)
For additional information on UI generation, refer to the following topics: List View Column Generation and View Items Layout Generation.
有关UI生成的其他信息,请参阅以下主题:列表视图列生成和视图项布局生成。

Next Lesson(下一课)

Customize the Application UI and Behavior
自定义应用程序UI和行为

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/47466.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cookies和session的区别【面试】

一、 共同点&#xff1a; 目的&#xff1a;Cookie和Session都是用来跟踪浏览器用户身份的会话方式。 二、 工作原理&#xff1a; 1. Cookie的工作原理 浏览器端第一次发送请求到服务器端。服务器端创建Cookie&#xff0c;包含用户信息&#xff0c;然后将Cookie发送到浏览器…

基于AT89C51单片机的多功能自行车测速计程器(含文档、源码与proteus仿真,以及系统详细介绍)

本篇文章论述的是基于AT89C51单片机的多功能自行车测速计程器的详情介绍&#xff0c;如果对您有帮助的话&#xff0c;还请关注一下哦&#xff0c;如果有资源方面的需要可以联系我。 目录 选题背景 原理图 PCB图 仿真图 代码 系统论文 资源下载 选题背景 美丽的夜晚&…

力扣刷题(自用)

哈希 128.最长连续序列 128. 最长连续序列 - 力扣&#xff08;LeetCode&#xff09; 这个题要求O(n)的时间复杂度&#xff0c;我一开始想的是双指针算法&#xff08;因为我并不是很熟悉set容器的使用&#xff09;&#xff0c;但是双指针算法有小部分数据过不了。 题解给的哈…

JavaScript:移除元素

这是原题&#xff1a;给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。 假设 nums 中不等于 val 的元素数量为 k&#xff0c;要通过此题&#xff0c;您需要执行以下操…

【PyTorch][chapter 26][李宏毅深度学习][attention-2]

前言&#xff1a; Multi-Head Attention 主要作用&#xff1a;将Q,K,V向量分成多个头&#xff0c;形成多个子语义空间&#xff0c;可以让模型去关注不同维度语义空间的信息 目录&#xff1a; attention 机制 Multi-Head Attention 一 attention 注意力 Self-Attention&#x…

三分钟速通银行家算法

银行家算法&#xff08;Bankers Algorithm&#xff09;是一种用于避免死锁的经典算法&#xff0c;广泛应用于操作系统、数据库管理系统及分布式系统中。下面将结合实战场景&#xff0c;详细介绍银行家算法的实现过程和应用。 一、算法背景 银行家算法由荷兰计算机科学家Edsge…

什么是im即时通讯?WorkPlus im即时通讯私有化部署安全可控

IM即时通讯是Instant Messaging的缩写&#xff0c;指的是一种实时的、即时的电子信息交流方式&#xff0c;也被称为即时通讯。它通过互联网和移动通信网络&#xff0c;使用户能够及时交换文本消息、语音通话、视频通话、文件共享等信息。而WorkPlus im即时通讯私有化部署则提供…

防火墙--双机热备

目录 双击热备作用 防火墙和路由器备份不同之处 如何连线 双机 热备 冷备 VRRP VGMP&#xff08;华为私有协议&#xff09; 场景解释 VGMP作用过程 主备的形成场景 接口故障的切换场景 整机故障 原主设备故障恢复的场景 如果没有开启抢占 如果开启了抢占 负载分…

对红酒品质进行数据分析(python)

http://t.csdnimg.cn/UWg2S 数据来源于这篇博客&#xff0c;直接下载好csv文件。 这篇内容均在VScode的jupyter notebook上完成&#xff0c;操作可以看我的另一篇博客&#xff1a;http://t.csdnimg.cn/69sDJ 一、准备工作 1. 导入数据库 #功能是可以内嵌绘图&#xff0c;并…

如何查看Linux中某个项目是否在Docker中运行

方法一&#xff1a;检查进程的 cgroup Docker 容器的进程运行在特定的 cgroup 中。你可以通过检查进程的 cgroup 信息来判断它是否在 Docker 容器中运行。 找到项目的进程 ID (PID)&#xff1a; 假设你知道项目的进程名称&#xff0c;例如 my_project&#xff0c;你可以使用 p…

纯硬件一键开关机电路的工作原理

这是一个一键开关机电路: 当按一下按键然后松开&#xff0c;MOS管导通&#xff0c;VOUT等于电源电压; 当再次按一下按键然后松开&#xff0c;MOS管关闭&#xff0c;VOUT等于0; 下面来分析一下这个电路的工作原理。上电后&#xff0c;输入电压通过R1和R2给电容充电&#xff0c;最…

继承和多态常见的面试问题

文章目录 概念问答 概念 下面哪种面向对象的方法可以让你变得富有( A) A: 继承 B: 封装 C: 多态 D: 抽象 (D )是面向对象程序设计语言中的一种机制。这种机制实现了方法的定义与具体的对象无关&#xff0c; 而对方法的调用则可以关联于具体的对象。 A: 继承 B: 模板 C: 对象的…

Java的Cpp本地库调用

叠甲&#xff1a;以下文章主要是依靠我的实际编码学习中总结出来的经验之谈&#xff0c;求逻辑自洽&#xff0c;不能百分百保证正确&#xff0c;有错误、未定义、不合适的内容请尽情指出&#xff01; 文章目录 1.设置项目文件结构2.生成 Cpp 头文件3.编写 Cpp 程序实现4.生成本…

TS相较于JS有什么优缺点

TypeScript&#xff08;TS&#xff09;是JavaScript的一个超集&#xff0c;它添加了静态类型检查和编译时的强大功能&#xff0c;目的是提高代码质量和维护性。相较于JavaScript&#xff0c;TS的主要优点和缺点如下&#xff1a; 优点&#xff1a; 类型安全性&#xff1a;通过…

如何让公众号文章排版变的高大上?

有的时候&#xff0c;你可能会疑惑&#xff0c;为什么你写的文章没人看&#xff1f;明明投入很多精力在标题和文章内容上&#xff0c;但收效甚微。 有一个关键性的因素可能被你忽略了&#xff0c;那就是排版&#xff0c;文章没有排版&#xff0c;无论你的内容再怎么精彩&#x…

力扣622.设计循环队列

力扣622.设计循环队列 通过数组索引构建一个虚拟的首尾相连的环当front rear时 队列为空当front rear 1时 队列为满 (最后一位不存) class MyCircularQueue {int front;int rear;int capacity;vector<int> elements;public:MyCircularQueue(int k) {//最后一位不存…

介绍一些编程语言— Mojo 语言

介绍一些编程语言— Mojo 语言 Mojo 语言 Mojo 官网 GitHub 简介 Mojo 语言是一种专为编写人工智能软件设计的编程语言&#xff0c;由 Modular Inc. 开发并宣布在 2024 2024 2024 年 3 3 3 月 29 29 29 日开源其核心组件。Mojo 旨在提供一种易于使用的语法&#xff0c;类…

智能化革新:智能AI如何助力生产力发展的未来与应用

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 前言 在当今这个科技飞速发展的时代&#xff0c;人工智能&#xff08;AI&#xff09;已经成为了推动生产力发展的重要力量。AI技…

深度学习落地实战:人流量监测

前言 大家好,我是机长 本专栏将持续收集整理市场上深度学习的相关项目,旨在为准备从事深度学习工作或相关科研活动的伙伴,储备、提升更多的实际开发经验,每个项目实例都可作为实际开发项目写入简历,且都附带完整的代码与数据集。可通过百度云盘进行获取,实现开箱即用 …

2024 睿抗机器人开发者大赛CAIP-编程技能赛-本科组(省赛)

RC-u1 热҈热҈热҈ 分数 10 全屏浏览 切换布局 作者 DAI, Longao 单位 杭州百腾教育科技有限公司 热҈热҈热҈……最近热得打的字都出汗了&#xff01; 幸好某连锁餐厅开启了气温大于等于 35 度即可获得一杯免费雪碧的活动。但不知为何&#xff0c;在每个星期四的时候&#x…